Skip to content

Commit 7e6ccb2

Browse files
authored
Merge pull request #1491 from mbroadst/NODE-953
fix(collection): pass batchSize to AggregationCursor
2 parents 3465090 + b5c188b commit 7e6ccb2

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/collection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var checkCollectionName = require('./utils').checkCollectionName
2121
, Cursor = require('./cursor')
2222
, unordered = require('./bulk/unordered')
2323
, ordered = require('./bulk/ordered')
24-
, assign = require('./utils').assign;
24+
, assign = require('./utils').assign
25+
, mergeOptions = require('./utils').mergeOptions;
2526

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

26732675
// Allow disk usage command

test/functional/aggregation_tests.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,42 @@ exports['Should correctly handle ISODate date matches in aggregation framework']
827827
// DOC_END
828828
}
829829
}
830+
831+
exports['should maintain batch size between calls to receive new batches'] = {
832+
metadata: {
833+
requires: { generators: true, topology: 'single' , node: ">0.10.0" }
834+
},
835+
836+
// The actual test we wish to run
837+
test: function(configure, test) {
838+
var co = require('co');
839+
840+
co(function*() {
841+
var instance = configure.newDbInstance({ w: 1 }, { poolSize: 1 });
842+
var db = yield instance.open();
843+
844+
var docs = [ { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 } ];
845+
var collection = db.collection('batchSizeContinue');
846+
yield collection.insertMany(docs, { w: 1 });
847+
var cursor = collection.aggregate([
848+
{ $match: { a: 1 } }, { $limit: 6 }
849+
], {
850+
cursor: { batchSize: 2 }
851+
});
852+
853+
var count = 0;
854+
while (yield cursor.hasNext()) {
855+
var data = yield cursor.next();
856+
test.equal(data.a, 1);
857+
858+
// ensure batch size is as specified
859+
test.equal(cursor.cursorState.documents.length, 2);
860+
count++;
861+
}
862+
863+
test.equal(count, 6);
864+
db.close();
865+
test.done();
866+
});
867+
}
868+
}

0 commit comments

Comments
 (0)