Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
6 changes: 3 additions & 3 deletions package.js
Original file line number Diff line number Diff line change
@@ -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"
});
Expand All @@ -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
Expand All @@ -24,7 +24,7 @@ Package.onTest(function(api) {
function configurePackage(api) {
api.versionsFrom('[email protected]');
api.use(['mongo-livedata', 'meteorhacks:[email protected]'], ['server']);

// common before
api.addFiles([
'index.js',
Expand Down
25 changes: 24 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,27 @@ Tinytest.add("using some options", function(test) {
], options);

test.equal(typeof result[0]['$cursor'], 'object');
});
});

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}]);
});