Skip to content

Commit 3054f1a

Browse files
committed
fix(collection): make the parameters of findOne very explicit
`Collection.prototype.findOne` needs to use the new explicit parameters for `find` (since it internally delegates). In the process it made sense to clean up the methods own parameter handling code as well
1 parent e0ca1b4 commit 3054f1a

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

lib/collection.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1331,17 +1331,21 @@ define.classMethod('save', { callback: true, promise: true });
13311331
* @param {Collection~resultCallback} [callback] The command result callback
13321332
* @return {Promise} returns Promise if no callback passed
13331333
*/
1334-
Collection.prototype.findOne = function() {
1335-
var args = Array.prototype.slice.call(arguments, 0);
1336-
var callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
1337-
return executeOperation(this.s.topology, findOne, [this, args, callback]);
1334+
Collection.prototype.findOne = function(query, options, callback) {
1335+
if (typeof query === 'function') (callback = query), (query = {}), (options = {});
1336+
if (typeof options === 'function') (callback = options), (options = {});
1337+
query = query || {};
1338+
options = options || {};
1339+
1340+
return executeOperation(this.s.topology, findOne, [this, query, options, callback]);
13381341
};
13391342

1340-
var findOne = function(self, args, callback) {
1341-
var cursor = self.find
1342-
.apply(self, args)
1343+
var findOne = function(self, query, options, callback) {
1344+
const cursor = self
1345+
.find(query, options)
13431346
.limit(-1)
13441347
.batchSize(1);
1348+
13451349
// Return the item
13461350
cursor.next(function(err, item) {
13471351
if (err != null) return handleCallback(callback, toError(err), null);

test/functional/causal_consistency_tests.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Causal Consistency', function() {
3535

3636
return db
3737
.collection('causal_test')
38-
.findOne({}, null, { session: session })
38+
.findOne({}, { session: session })
3939
.then(() => {
4040
expect(test.commands.started).to.have.length(1);
4141
expect(test.commands.succeeded).to.have.length(1);
@@ -56,7 +56,7 @@ describe('Causal Consistency', function() {
5656

5757
return db
5858
.collection('causal_test')
59-
.findOne({}, null, { session: session })
59+
.findOne({}, { session: session })
6060
.then(() => {
6161
expect(test.commands.started).to.have.length(1);
6262
expect(test.commands.succeeded).to.have.length(1);
@@ -79,13 +79,13 @@ describe('Causal Consistency', function() {
7979
let firstOperationTime;
8080
return db
8181
.collection('causal_test')
82-
.findOne({}, null, { session: session })
82+
.findOne({}, { session: session })
8383
.then(() => {
8484
const firstFindCommand = test.commands.started[0].command;
8585
expect(firstFindCommand).to.not.have.key('readConcern');
8686
firstOperationTime = test.commands.succeeded[0].reply.operationTime;
8787

88-
return db.collection('causal_test').findOne({}, null, { session: session });
88+
return db.collection('causal_test').findOne({}, { session: session });
8989
})
9090
.then(() => {
9191
const secondFindCommand = test.commands.started[1].command;
@@ -107,8 +107,8 @@ describe('Causal Consistency', function() {
107107
const coll = db.collection('causal_test', { readConcern: { level: 'majority' } });
108108

109109
return coll
110-
.findOne({}, null, { session: session })
111-
.then(() => coll.findOne({}, null, { session: session }))
110+
.findOne({}, { session: session })
111+
.then(() => coll.findOne({}, { session: session }))
112112
.then(() => {
113113
const command = test.commands.started[1].command;
114114
expect(command).to.have.any.key('readConcern');
@@ -133,7 +133,7 @@ describe('Causal Consistency', function() {
133133
.insert({}, { session: session })
134134
.then(() => {
135135
firstOperationTime = test.commands.succeeded[0].reply.operationTime;
136-
return db.collection('causal_test').findOne({}, null, { session: session });
136+
return db.collection('causal_test').findOne({}, { session: session });
137137
})
138138
.then(() => {
139139
const secondFindCommand = test.commands.started[1].command;

0 commit comments

Comments
 (0)