Skip to content

Commit

Permalink
Make aggregate return a stream. Fixes #154.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorribas committed Nov 7, 2014
1 parent c533e5a commit 6d331ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var thunky = require('thunky');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Readable = require('stream').Readable || require('readable-stream');
var PassThrough = require('stream').PassThrough || require('readable-stream').PassThrough;

var DriverCollection = mongodb.Collection.prototype;
var DriverDb = mongodb.Db.prototype;
Expand Down Expand Up @@ -198,7 +199,19 @@ var Collection = function(name, oncollection) {
};

Collection.prototype.aggregate = function() {
return this._apply(DriverCollection.aggregate, ensureCallback(arguments));
var args = Array.prototype.slice.call(arguments);
if (typeof args[args.length-1] === 'function') {
return this._apply(DriverCollection.aggregate, ensureCallback(arguments));
}
args.push({cursor: {batchSize: 1}});

var pt = new PassThrough({objectMode: true, highWaterMark: 16});
this._get(function(err, collection) {
if (err) return callback(err);
collection.aggregate.apply(collection, args).pipe(pt);
});

return pt;
};

Collection.prototype.count = function() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"test": "tape test/test-*.js; echo \"Harmony tests\"; node --harmony node_modules/tape/bin/tape test/test-*.js"
},
"devDependencies": {
"concat-stream": "^1.4.6",
"each-series": "^0.2.0",
"tape": "^2.13.4"
}
Expand Down

0 comments on commit 6d331ce

Please sign in to comment.