Skip to content

Commit

Permalink
fix(aggregation): ensure that the cursor key is always present
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Aug 9, 2017
1 parent 3a5232a commit f16f314
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ var checkCollectionName = require('./utils').checkCollectionName
, Cursor = require('./cursor')
, unordered = require('./bulk/unordered')
, ordered = require('./bulk/ordered')
, assign = require('./utils').assign
, mergeOptions = require('./utils').mergeOptions;
, assign = require('./utils').assign;

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

// If explain has been specified add it
if(options.explain) command.explain = options.explain;
if (options.explain) command.explain = options.explain;

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

if (this.s.topology.capabilities().hasAggregationCursor) {
options.cursor = options.cursor || { batchSize : 1000 };
command.cursor = options.cursor;
}

// promiseLibrary
options.promiseLibrary = this.s.promiseLibrary;

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

if(this.s.topology.capabilities().hasAggregationCursor) {
options.cursor = options.cursor || { batchSize : 1000 };
command.cursor = options.cursor;
}

// Allow disk usage command
if(typeof options.allowDiskUse == 'boolean') command.allowDiskUse = options.allowDiskUse;
if(typeof options.maxTimeMS == 'number') command.maxTimeMS = options.maxTimeMS;
Expand All @@ -2686,12 +2685,18 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
return this.s.topology.cursor(this.s.namespace, command, options);
}

// We do not allow cursor
if(options.cursor) {
return this.s.topology.cursor(this.s.namespace, command, options);
if (options.cursor) {
var cursor = this.s.topology.cursor(this.s.namespace, command, options);
return cursor.toArray(function(err, result) {
if (err) {
return handleCallback(callback, err);
}

handleCallback(callback, null, result);
});
}

// Execute the command
// For legacy server versions, we execute the command and format the result
this.s.db.command(command, options, function(err, result) {
if(err) {
handleCallback(callback, err);
Expand Down

0 comments on commit f16f314

Please sign in to comment.