Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added mapReduce #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
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}]);
});