@@ -21,8 +21,7 @@ var checkCollectionName = require('./utils').checkCollectionName
21
21
, Cursor = require ( './cursor' )
22
22
, unordered = require ( './bulk/unordered' )
23
23
, ordered = require ( './bulk/ordered' )
24
- , assign = require ( './utils' ) . assign
25
- , mergeOptions = require ( './utils' ) . mergeOptions ;
24
+ , assign = require ( './utils' ) . assign ;
26
25
27
26
/**
28
27
* @fileOverview The **Collection** class is an internal class that embodies a MongoDB collection
@@ -2656,13 +2655,18 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
2656
2655
options = getReadPreference ( this , options , this . s . db , this ) ;
2657
2656
2658
2657
// If explain has been specified add it
2659
- if ( options . explain ) command . explain = options . explain ;
2658
+ if ( options . explain ) command . explain = options . explain ;
2660
2659
2661
2660
// Validate that cursor options is valid
2662
2661
if ( options . cursor != null && typeof options . cursor != 'object' ) {
2663
2662
throw toError ( 'cursor options must be an object' ) ;
2664
2663
}
2665
2664
2665
+ if ( this . s . topology . capabilities ( ) . hasAggregationCursor ) {
2666
+ options . cursor = options . cursor || { batchSize : 1000 } ;
2667
+ command . cursor = options . cursor ;
2668
+ }
2669
+
2666
2670
// promiseLibrary
2667
2671
options . promiseLibrary = this . s . promiseLibrary ;
2668
2672
@@ -2673,11 +2677,6 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
2673
2677
throw new MongoError ( 'cannot connect to server' ) ;
2674
2678
}
2675
2679
2676
- if ( this . s . topology . capabilities ( ) . hasAggregationCursor ) {
2677
- options . cursor = options . cursor || { batchSize : 1000 } ;
2678
- command . cursor = options . cursor ;
2679
- }
2680
-
2681
2680
// Allow disk usage command
2682
2681
if ( typeof options . allowDiskUse == 'boolean' ) command . allowDiskUse = options . allowDiskUse ;
2683
2682
if ( typeof options . maxTimeMS == 'number' ) command . maxTimeMS = options . maxTimeMS ;
@@ -2686,12 +2685,18 @@ Collection.prototype.aggregate = function(pipeline, options, callback) {
2686
2685
return this . s . topology . cursor ( this . s . namespace , command , options ) ;
2687
2686
}
2688
2687
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
+ } ) ;
2692
2697
}
2693
2698
2694
- // Execute the command
2699
+ // For legacy server versions, we execute the command and format the result
2695
2700
this . s . db . command ( command , options , function ( err , result ) {
2696
2701
if ( err ) {
2697
2702
handleCallback ( callback , err ) ;
0 commit comments