Skip to content

fix(collection): pass batchSize to AggregationCursor #1491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var checkCollectionName = require('./utils').checkCollectionName
, Cursor = require('./cursor')
, unordered = require('./bulk/unordered')
, ordered = require('./bulk/ordered')
, assign = require('./utils').assign;
, assign = require('./utils').assign
, mergeOptions = require('./utils').mergeOptions;

/**
* @fileOverview The **Collection** class is an internal class that embodies a MongoDB collection
Expand Down Expand Up @@ -2668,6 +2669,7 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
if(this.s.topology.capabilities().hasAggregationCursor) {
options.cursor = options.cursor || { batchSize : 1000 };
command.cursor = options.cursor;
mergeOptions(options, options.cursor);
}

// Allow disk usage command
Expand Down
39 changes: 39 additions & 0 deletions test/functional/aggregation_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,42 @@ exports['Should correctly handle ISODate date matches in aggregation framework']
// DOC_END
}
}

exports['should maintain batch size between calls to receive new batches'] = {
metadata: {
requires: { generators: true, topology: 'single' , node: ">0.10.0" }
},

// The actual test we wish to run
test: function(configure, test) {
var co = require('co');

co(function*() {
var instance = configure.newDbInstance({ w: 1 }, { poolSize: 1 });
var db = yield instance.open();

var docs = [ { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 } ];
var collection = db.collection('batchSizeContinue');
yield collection.insertMany(docs, { w: 1 });
var cursor = collection.aggregate([
{ $match: { a: 1 } }, { $limit: 6 }
], {
cursor: { batchSize: 2 }
});

var count = 0;
while (yield cursor.hasNext()) {
var data = yield cursor.next();
test.equal(data.a, 1);

// ensure batch size is as specified
test.equal(cursor.cursorState.documents.length, 2);
count++;
}

test.equal(count, 6);
db.close();
test.done();
});
}
}