From f86daeab5efcc4771eab4d1ea91fa11d642a2316 Mon Sep 17 00:00:00 2001 From: Robert Simon Date: Thu, 20 Aug 2015 19:07:17 +0200 Subject: [PATCH 1/2] added mapReduce --- index.js | 11 +++++++++++ test.js | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 62bd4ed..3750bf4 100644 --- a/index.js +++ b/index.js @@ -10,3 +10,14 @@ Mongo.Collection.prototype.aggregate = function(pipelines, options) { } 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/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}]); +}); From 1363061667397b70c6907a30b807c9e491174f03 Mon Sep 17 00:00:00 2001 From: jm Date: Sun, 26 Aug 2018 11:03:33 +0800 Subject: [PATCH 2/2] update meteor1.7 --- index.js | 9 +++++++-- package.js | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 3750bf4..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,7 +8,12 @@ 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; 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',