Skip to content

Commit

Permalink
NODE-993 Expose hasNext for command cursor and add docs for both Comm…
Browse files Browse the repository at this point in the history
…andCursor and Aggregation Cursor
  • Loading branch information
christkv committed May 22, 2017
1 parent a37812d commit 504d401
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/aggregation_cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
define.classMethod('toArray', {callback: true, promise:true});
define.classMethod('each', {callback: true, promise:false});
define.classMethod('forEach', {callback: true, promise:false});
define.classMethod('hasNext', {callback: true, promise:true});
define.classMethod('next', {callback: true, promise:true});
define.classMethod('close', {callback: true, promise:true});
define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
Expand All @@ -331,6 +332,14 @@ define.classMethod('readBufferedDocuments', {callback: false, promise:false, ret
* @return {Promise} returns Promise if no callback passed
*/

/**
* Check if there is any document still available in the cursor
* @function AggregationCursor.prototype.hasNext
* @param {AggregationCursor~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/

/**
* The callback format for results
* @callback AggregationCursor~toArrayResultCallback
Expand Down
11 changes: 10 additions & 1 deletion lib/command_cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var CommandCursor = function(bson, ns, cmd, options, topology, topologyOptions)
inherits(CommandCursor, Readable);

// Set the methods to inherit from prototype
var methodsToInherit = ['_next', 'next', 'each', 'forEach', 'toArray'
var methodsToInherit = ['_next', 'next', 'hasNext', 'each', 'forEach', 'toArray'
, 'rewind', 'bufferedCount', 'readBufferedDocuments', 'close', 'isClosed', 'kill', 'setCursorBatchSize'
, '_find', '_getmore', '_killcursor', 'isDead', 'explain', 'isNotified', 'isKilled'];

Expand Down Expand Up @@ -207,6 +207,7 @@ define.classMethod('toArray', {callback: true, promise:true});
define.classMethod('each', {callback: true, promise:false});
define.classMethod('forEach', {callback: true, promise:false});
define.classMethod('next', {callback: true, promise:true});
define.classMethod('hasNext', {callback: true, promise:true});
define.classMethod('close', {callback: true, promise:true});
define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
define.classMethod('rewind', {callback: false, promise:false});
Expand All @@ -221,6 +222,14 @@ define.classMethod('readBufferedDocuments', {callback: false, promise:false, ret
* @return {Promise} returns Promise if no callback passed
*/

/**
* Check if there is any document still available in the cursor
* @function CommandCursor.prototype.hasNext
* @param {CommandCursor~resultCallback} [callback] The result callback.
* @throws {MongoError}
* @return {Promise} returns Promise if no callback passed
*/

/**
* The callback format for results
* @callback CommandCursor~toArrayResultCallback
Expand Down
47 changes: 47 additions & 0 deletions test/functional/aggregation_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,4 +826,51 @@ exports['Should correctly handle ISODate date matches in aggregation framework']
});
// DOC_END
}
}

/**
* Correctly call the aggregation framework to return a cursor with batchSize 1 and get the first result using next
*
* @ignore
*/
exports['Should correctly exercise hasNext function on aggregation cursor'] = {
metadata: {
requires: {
mongodb: ">=2.6.0"
, topology: 'single'
, node: ">0.10.0"
}
},

// The actual test we wish to run
test: function(configure, test) {
var db = configure.newDbInstance({w:1}, {poolSize:1});

// DOC_LINE var db = new Db('test', new Server('localhost', 27017));
// DOC_START
db.open(function(err, db) {
// Create a collection
var collection = db.collection('shouldCorrectlyQueryUsingISODate3');
// Insert the docs
collection.insertMany([
{a:1}, {b:1}
], {w: 1}, function(err, result) {

// Execute aggregate, notice the pipeline is expressed as an Array
var cursor = collection.aggregate([{
$match: {}
}]);

// Iterate over all the items in the cursor
cursor.hasNext(function(err, result) {
test.equal(null, err);
test.equal(true, result);

db.close();
test.done();
});
});
});
// DOC_END
}
}
23 changes: 23 additions & 0 deletions test/functional/index_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,29 @@ exports['should correctly use listIndexes to retrieve index list'] = {
}
}

exports['should correctly use listIndexes to retrieve index list using hasNext'] = {
metadata: { requires: { mongodb: ">=2.4.0", topology: ['single', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
db.open(function(err, db) {
db.collection('testListIndexes_2').ensureIndex({a:1}, function(err, r) {
test.equal(null, err);

// Get the list of indexes
db.collection('testListIndexes_2').listIndexes().hasNext(function(err, result) {
test.equal(null, err);
test.equal(true, result);

db.close();
test.done();
});
});
});
}
}

exports['should correctly ensureIndex for nested style index name c.d'] = {
metadata: { requires: { mongodb: ">=2.4.0", topology: ['single', 'ssl', 'heap', 'wiredtiger'] } },

Expand Down

0 comments on commit 504d401

Please sign in to comment.