Skip to content

Commit

Permalink
Merge pull request #98 from sevifives/448e41deb6c790fb299546221e0c91b…
Browse files Browse the repository at this point in the history
…e39a463c1

Add group and tests for group
  • Loading branch information
marcello3d committed May 30, 2012
2 parents c41c213 + 448e41d commit f1a6bdc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -274,7 +274,7 @@ From http://api.mongodb.org/js/1.8.1/symbols/src/shell_collection.js.html
})</code>
+ <code><strong><em>collection.getDB</em></strong>()</code> get DB object associated with collection implemented as <code><strong>collection.db</strong></code>
+ <code><strong><em>collection.getIndexes</em></strong>()</code> implemented as <code><strong>collection.indexes</strong>(callback)</code>
+ <code>collection.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )</code>
+ <code><strong>collection.group</strong>( { key : ..., initial: ..., reduce : ...[, cond: ...] } )</code>
+ <code><strong>collection.mapReduce</strong>( mapFunction , reduceFunction , [optional params][, callback])</code>
+ <code><strong>collection.remove</strong>(query[, callback])</code> - see callback note below
+ <code>collection.renameCollection( newName , [dropTarget] )</code> renames the collection.
Expand Down
4 changes: 4 additions & 0 deletions lib/collection.js
Expand Up @@ -275,6 +275,10 @@ MongolianCollection.prototype.findAndModify = function(options, callback) {
}))
}

MongolianCollection.prototype.group = function (command, callback) {
this.db.runCommand({group: command}, callback);
}

MongolianCollection.prototype.distinct = function(key, query, callback) {
if (!callback && typeof query === 'function') {
callback = query
Expand Down
19 changes: 19 additions & 0 deletions test/collection1000.js
Expand Up @@ -52,6 +52,25 @@ module.exports = {
test.done()
})
},
"group check": function (test) {
collection.group({
'ns': 'test_1000',
'key':'even',
'initial': {evenCount:0, oddCount: 0, total:0},
'$reduce': function (doc, out) {
++out[doc.even ? 'evenCount' : 'oddCount'];
++out.total;
},
finalize: function (out) {}
}, function (error,group) {
var ret = group.retval[0];
test.ifError(error);
test.equal(ret.total, 1000);
test.equal(ret.evenCount, 500);
test.equal(ret.oddCount,500);
test.done();
});
},
"mapped forEach": function(test) {
var counter = 0
collection.find().map(function(item) {
Expand Down

0 comments on commit f1a6bdc

Please sign in to comment.