From 21263cad02124f0c1f8d000334caa85c8630fc6c Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 30 Jun 2021 16:52:02 +0200 Subject: [PATCH] feat(shell-api): allow options in find() and findOne() MONGOSH-857 Allow passing options through to the driver for `coll.find()` and `coll.findOne()`. --- packages/shell-api/src/collection.ts | 6 ++-- packages/shell-api/src/integration.spec.ts | 38 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/shell-api/src/collection.ts b/packages/shell-api/src/collection.ts index 284dfa08c4..ce83562548 100644 --- a/packages/shell-api/src/collection.ts +++ b/packages/shell-api/src/collection.ts @@ -406,8 +406,7 @@ export default class Collection extends ShellApiWithMongoClass { */ @returnType('Cursor') @apiVersions([1]) - find(query?: Document, projection?: Document): Cursor { - const options: FindOptions = {}; + find(query?: Document, projection?: Document, options: FindOptions = {}): Cursor { if (projection) { options.projection = projection; } @@ -463,8 +462,7 @@ export default class Collection extends ShellApiWithMongoClass { @returnsPromise @returnType('Document') @apiVersions([1]) - async findOne(query: Document = {}, projection?: Document): Promise { - const options: any = {}; + async findOne(query: Document = {}, projection?: Document, options: FindOptions = {}): Promise { if (projection) { options.projection = projection; } diff --git a/packages/shell-api/src/integration.spec.ts b/packages/shell-api/src/integration.spec.ts index 0d3c5a1e04..f3bdfca5a0 100644 --- a/packages/shell-api/src/integration.spec.ts +++ b/packages/shell-api/src/integration.spec.ts @@ -933,6 +933,44 @@ describe('Shell API (integration)', function() { }); }); }); + + describe('find', () => { + it('uses default options for the driver (find)', async() => { + const longOne = new serviceProvider.bsonLibrary.Long('1'); + await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 }); + + const cursor = await collection.find({}); + + expect(await cursor.toArray()).to.deep.equal([{ longOne, _id: 0 }]); + }); + + it('passes through options to the driver (find)', async() => { + const longOne = new serviceProvider.bsonLibrary.Long('1'); + await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 }); + + const cursor = await collection.find({}, {}, { promoteLongs: true }); + + expect(await cursor.toArray()).to.deep.equal([{ longOne: 1, _id: 0 }]); + }); + + it('uses default options for the driver (findOne)', async() => { + const longOne = new serviceProvider.bsonLibrary.Long('1'); + await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 }); + + const doc = await collection.findOne({}); + + expect(doc).to.deep.equal({ longOne, _id: 0 }); + }); + + it('passes through options to the driver (findOne)', async() => { + const longOne = new serviceProvider.bsonLibrary.Long('1'); + await serviceProvider.insertOne(dbName, collectionName, { longOne, _id: 0 }); + + const doc = await collection.findOne({}, {}, { promoteLongs: true }); + + expect(doc).to.deep.equal({ longOne: 1, _id: 0 }); + }); + }); }); describe('db', () => {