Skip to content

Commit f16f314

Browse files
committed
fix(aggregation): ensure that the cursor key is always present
NODE-1117
1 parent 3a5232a commit f16f314

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lib/collection.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ var checkCollectionName = require('./utils').checkCollectionName
2121
, Cursor = require('./cursor')
2222
, unordered = require('./bulk/unordered')
2323
, ordered = require('./bulk/ordered')
24-
, assign = require('./utils').assign
25-
, mergeOptions = require('./utils').mergeOptions;
24+
, assign = require('./utils').assign;
2625

2726
/**
2827
* @fileOverview The **Collection** class is an internal class that embodies a MongoDB collection
@@ -2656,13 +2655,18 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
26562655
options = getReadPreference(this, options, this.s.db, this);
26572656

26582657
// If explain has been specified add it
2659-
if(options.explain) command.explain = options.explain;
2658+
if (options.explain) command.explain = options.explain;
26602659

26612660
// Validate that cursor options is valid
26622661
if(options.cursor != null && typeof options.cursor != 'object') {
26632662
throw toError('cursor options must be an object');
26642663
}
26652664

2665+
if (this.s.topology.capabilities().hasAggregationCursor) {
2666+
options.cursor = options.cursor || { batchSize : 1000 };
2667+
command.cursor = options.cursor;
2668+
}
2669+
26662670
// promiseLibrary
26672671
options.promiseLibrary = this.s.promiseLibrary;
26682672

@@ -2673,11 +2677,6 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
26732677
throw new MongoError('cannot connect to server');
26742678
}
26752679

2676-
if(this.s.topology.capabilities().hasAggregationCursor) {
2677-
options.cursor = options.cursor || { batchSize : 1000 };
2678-
command.cursor = options.cursor;
2679-
}
2680-
26812680
// Allow disk usage command
26822681
if(typeof options.allowDiskUse == 'boolean') command.allowDiskUse = options.allowDiskUse;
26832682
if(typeof options.maxTimeMS == 'number') command.maxTimeMS = options.maxTimeMS;
@@ -2686,12 +2685,18 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
26862685
return this.s.topology.cursor(this.s.namespace, command, options);
26872686
}
26882687

2689-
// We do not allow cursor
2690-
if(options.cursor) {
2691-
return this.s.topology.cursor(this.s.namespace, command, options);
2688+
if (options.cursor) {
2689+
var cursor = this.s.topology.cursor(this.s.namespace, command, options);
2690+
return cursor.toArray(function(err, result) {
2691+
if (err) {
2692+
return handleCallback(callback, err);
2693+
}
2694+
2695+
handleCallback(callback, null, result);
2696+
});
26922697
}
26932698

2694-
// Execute the command
2699+
// For legacy server versions, we execute the command and format the result
26952700
this.s.db.command(command, options, function(err, result) {
26962701
if(err) {
26972702
handleCallback(callback, err);

0 commit comments

Comments
 (0)