Skip to content

Commit 42bf900

Browse files
committed
Optional callbacks on find, close and collectionsInfo methods
1 parent 520bb92 commit 42bf900

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

integration/integration_tests.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,33 @@ var all_tests = {
535535
});
536536
},
537537

538+
// Test a simple find chained
539+
test_find_simple_chained : function() {
540+
client.createCollection('test_find_simple_chained', function(err, r) {
541+
var collection = client.collection('test_find_simple_chained', function(err, collection) {
542+
var doc1 = null;
543+
var doc2 = null;
544+
545+
// Insert some test documents
546+
collection.insert([{a:2}, {b:3}], function(err, docs) {doc1 = docs[0]; doc2 = docs[1]});
547+
// Ensure correct insertion testing via the cursor and the count function
548+
collection.find().toArray(function(err, documents) {
549+
test.equal(2, documents.length);
550+
});
551+
collection.count(function(err, count) {
552+
test.equal(2, count);
553+
});
554+
// Fetch values by selection
555+
collection.find({'a': doc1.a}).toArray(function(err, documents) {
556+
test.equal(1, documents.length);
557+
test.equal(doc1.a, documents[0].a);
558+
// Let's close the db
559+
finished_test({test_find_simple_chained:'ok'});
560+
});
561+
});
562+
});
563+
},
564+
538565
// Test advanced find
539566
test_find_advanced : function() {
540567
client.createCollection('test_find_advanced', function(err, r) {

lib/mongodb/collection.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -347,37 +347,41 @@ Collection.prototype.findAndModify = function(query, sort, update, options, call
347347

348348
/**
349349
* Various argument possibilities
350-
* 1 callback
351-
* 2 selector, callback,
352-
* 2 callback, options // really?!
353-
* 3 selector, fields, callback
354-
* 3 selector, options, callback
355-
* 4,selector, fields, options, callback
356-
* 5 selector, fields, skip, limit, callback
357-
* 6 selector, fields, skip, limit, timeout, callback
350+
* 1 callback?
351+
* 2 selector, callback?,
352+
* 2 callback?, options // really?!
353+
* 3 selector, fields, callback?
354+
* 3 selector, options, callback?
355+
* 4,selector, fields, options, callback?
356+
* 5 selector, fields, skip, limit, callback?
357+
* 6 selector, fields, skip, limit, timeout, callback?
358358
*
359359
* Available options:
360360
* limit, sort, fields, skip, hint, explain, snapshot, timeout, tailable, batchSize
361361
*/
362362
Collection.prototype.find = function() {
363363
var options,
364-
len = arguments.length,
365-
selector = (len > 1) ? arguments[0] : {},
366-
fields = (len > 2) ? arguments[1] : undefined,
367-
callback = arguments[len-1];
368-
369-
if(len == 2 && typeof arguments[0] == 'function') {
370-
selector = {}; options = arguments[1]; callback = arguments[0];
364+
args = Array.prototype.slice.call(arguments, 0);
365+
has_callback = typeof args[args.length - 1] === 'function',
366+
has_weird_callback = typeof args[0] === 'function',
367+
callback = has_callback ? args.pop() : (has_weird_callback ? args.shift() : null),
368+
len = args.length,
369+
selector = (len >= 1) ? args[0] : {},
370+
fields = (len >= 2) ? args[1] : undefined;
371+
372+
if(len == 1 && has_weird_callback) { // backwards compat for callback?, options case
373+
selector = {};
374+
options = args[0];
371375
}
372376

373-
if(len == 3){ // backwards compat for options object
377+
if(len == 2){ // backwards compat for options object
374378
var test = ['limit','sort','fields','skip','hint','explain','snapshot','timeout','tailable', 'batchSize'],
375379
idx = 0, l = test.length, is_option = false;
376380
while(!is_option && idx < l) if(test[idx] in fields ) is_option = true; else idx++;
377381
options = is_option ? fields : {};
378382
if(is_option) fields = undefined;
379383
}
380-
if(len == 4) options = arguments[2];
384+
if(len == 3) options = args[2];
381385

382386
if(options && options.fields){
383387
fields = {};
@@ -389,13 +393,19 @@ Collection.prototype.find = function() {
389393
}
390394
if(!options) options = {};
391395

392-
options.skip = len > 4 ? arguments[2] : options.skip ? options.skip : 0;
393-
options.limit = len > 4 ? arguments[3] : options.limit ? options.limit : 0;
396+
options.skip = len > 3 ? args[2] : options.skip ? options.skip : 0;
397+
options.limit = len > 3 ? args[3] : options.limit ? options.limit : 0;
394398
options.hint = options.hint != null ? this.normalizeHintField(options.hint) : this.internalHint;
395-
options.timeout = len == 6 ? arguments[4] : options.timeout ? options.timeout : undefined;
399+
options.timeout = len == 5 ? args[4] : options.timeout ? options.timeout : undefined;
396400

397401
var o = options;
398-
callback(null, new Cursor(this.db, this, selector, fields, o.skip, o.limit, o.sort, o.hint, o.explain, o.snapshot, o.timeout, o.tailable, o.batchSize));
402+
403+
// callback for backward compatibility
404+
if (callback) {
405+
callback(null, new Cursor(this.db, this, selector, fields, o.skip, o.limit, o.sort, o.hint, o.explain, o.snapshot, o.timeout, o.tailable, o.batchSize));
406+
} else {
407+
return new Cursor(this.db, this, selector, fields, o.skip, o.limit, o.sort, o.hint, o.explain, o.snapshot, o.timeout, o.tailable, o.batchSize);
408+
}
399409
};
400410

401411
Collection.prototype.normalizeHintField = function(hint) {

lib/mongodb/cursor.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,20 @@ Cursor.prototype.close = function(callback) {
624624
if(this.cursorId instanceof self.db.bson_serializer.Long && this.cursorId.greaterThan(self.db.bson_serializer.Long.fromInt(0))) {
625625
try {
626626
var command = new KillCursorCommand(this.db, [this.cursorId]);
627-
this.db.executeCommand(command, null);
627+
this.db.executeCommand(command, null);
628628
} catch(err) {}
629629
}
630630

631631
this.cursorId = self.db.bson_serializer.Long.fromInt(0);
632632
this.state = Cursor.CLOSED;
633-
if (callback) callback(null, this);
634-
633+
634+
// callback for backward compatibility
635+
if (callback) {
636+
callback(null, this);
637+
} else {
638+
return this;
639+
}
640+
635641
this.items = null;
636642
};
637643

lib/mongodb/db.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,14 @@ Db.prototype.collectionsInfo = function(collection_name, callback) {
193193
var selector = {};
194194
// If we are limiting the access to a specific collection name
195195
if(collection_name != null) selector.name = this.databaseName + "." + collection_name;
196+
196197
// Return Cursor
197-
callback(null, new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector));
198+
// callback for backward compatibility
199+
if (callback) {
200+
callback(null, new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector));
201+
} else {
202+
return new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector);
203+
}
198204
};
199205

200206
/**

0 commit comments

Comments
 (0)