Skip to content

Commit 84032cb

Browse files
authored
feat(shell-api): allow options in find() and findOne() MONGOSH-857 (#975)
Allow passing options through to the driver for `coll.find()` and `coll.findOne()`.
1 parent 8778808 commit 84032cb

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

packages/shell-api/src/collection.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ export default class Collection extends ShellApiWithMongoClass {
406406
*/
407407
@returnType('Cursor')
408408
@apiVersions([1])
409-
find(query?: Document, projection?: Document): Cursor {
410-
const options: FindOptions = {};
409+
find(query?: Document, projection?: Document, options: FindOptions = {}): Cursor {
411410
if (projection) {
412411
options.projection = projection;
413412
}
@@ -463,8 +462,7 @@ export default class Collection extends ShellApiWithMongoClass {
463462
@returnsPromise
464463
@returnType('Document')
465464
@apiVersions([1])
466-
async findOne(query: Document = {}, projection?: Document): Promise<Document | null> {
467-
const options: any = {};
465+
async findOne(query: Document = {}, projection?: Document, options: FindOptions = {}): Promise<Document | null> {
468466
if (projection) {
469467
options.projection = projection;
470468
}

packages/shell-api/src/integration.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,44 @@ describe('Shell API (integration)', function() {
933933
});
934934
});
935935
});
936+
937+
describe('find', () => {
938+
it('uses default options for the driver (find)', async() => {
939+
const longOne = new serviceProvider.bsonLibrary.Long('1');
940+
await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 });
941+
942+
const cursor = await collection.find({});
943+
944+
expect(await cursor.toArray()).to.deep.equal([{ longOne, _id: 0 }]);
945+
});
946+
947+
it('passes through options to the driver (find)', async() => {
948+
const longOne = new serviceProvider.bsonLibrary.Long('1');
949+
await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 });
950+
951+
const cursor = await collection.find({}, {}, { promoteLongs: true });
952+
953+
expect(await cursor.toArray()).to.deep.equal([{ longOne: 1, _id: 0 }]);
954+
});
955+
956+
it('uses default options for the driver (findOne)', async() => {
957+
const longOne = new serviceProvider.bsonLibrary.Long('1');
958+
await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 });
959+
960+
const doc = await collection.findOne({});
961+
962+
expect(doc).to.deep.equal({ longOne, _id: 0 });
963+
});
964+
965+
it('passes through options to the driver (findOne)', async() => {
966+
const longOne = new serviceProvider.bsonLibrary.Long('1');
967+
await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 });
968+
969+
const doc = await collection.findOne({}, {}, { promoteLongs: true });
970+
971+
expect(doc).to.deep.equal({ longOne: 1, _id: 0 });
972+
});
973+
});
936974
});
937975

938976
describe('db', () => {

0 commit comments

Comments
 (0)