diff --git a/index.js b/index.js index 62bd4ed..9a484b5 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ wrapAsync = (Meteor.wrapAsync)? Meteor.wrapAsync : Meteor._wrapAsync; -Mongo.Collection.prototype.aggregate = function(pipelines, options) { +Mongo.Collection.prototype.aggregate = function(pipelines = [], options) { var coll; if (this.rawCollection) { // >= Meteor 1.0.4 @@ -8,5 +8,21 @@ Mongo.Collection.prototype.aggregate = function(pipelines, options) { // < Meteor 1.0.4 coll = this._getCollection(); } - return wrapAsync(coll.aggregate.bind(coll))(pipelines, options); + if(parseInt(MongoInternals.NpmModules.mongodb.version) >= 3) { + const cursor = wrapAsync(coll.aggregate, coll)(pipelines, options); + return wrapAsync(cursor.toArray, cursor)(); + } else { + return wrapAsync(coll.aggregate.bind(coll))(pipelines, options); + } +} +Mongo.Collection.prototype.mapReduce = function(mapFunction, reduceFunction, options) { + var coll; + if (this.rawCollection) { + // >= Meteor 1.0.4 + coll = this.rawCollection(); + } else { + // < Meteor 1.0.4 + coll = this._getCollection(); + } + return wrapAsync(coll.mapReduce.bind(coll))(mapFunction, reduceFunction, options); } diff --git a/package.js b/package.js index f923c30..46bf830 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ "summary": "Proper MongoDB aggregations support for Meteor", - "version": "1.3.0", + "version": "1.4.0", "git": "https://github.com/meteorhacks/meteor-aggregate.git", "name": "meteorhacks:aggregate" }); @@ -12,7 +12,7 @@ Package.onUse(function(api) { Package.onTest(function(api) { configurePackage(api); api.use([ - 'tinytest', 'accounts-password' + 'tinytest', 'accounts-password', 'random' ], ['server']); // common before @@ -24,7 +24,7 @@ Package.onTest(function(api) { function configurePackage(api) { api.versionsFrom('METEOR@1.0'); api.use(['mongo-livedata', 'meteorhacks:collection-utils@1.2.0'], ['server']); - + // common before api.addFiles([ 'index.js', diff --git a/test.js b/test.js index 960122a..72febf5 100644 --- a/test.js +++ b/test.js @@ -39,4 +39,27 @@ Tinytest.add("using some options", function(test) { ], options); test.equal(typeof result[0]['$cursor'], 'object'); -}); \ No newline at end of file +}); + +Tinytest.add("method mapReduce signature", function(test) { + var coll = new Mongo.Collection(Random.id()); + test.equal(typeof coll.mapReduce, 'function'); +}); + +Tinytest.add("let's mapReduce", function(test) { + var coll = new Mongo.Collection(Random.id()); + coll.insert({group: 1, value: 10}); + coll.insert({group: 1, value: 100}); + coll.insert({group: 2, value: 1000}); + + var mapFunction1 = function() { + emit(this.group, this.value); + }; + var reduceFunction1 = function(keyGroupId, values) { + return Array.sum(values); + }; + + var result = coll.mapReduce(mapFunction1, reduceFunction1, { out: { inline: 1 } }) + + test.equal(result, [{"_id":1,"value":110},{"_id":2,"value":1000}]); +});