Try with db.collection.group
For example, using this collection:
> db.foo.find() { "_id" : ObjectId("..."), "a" : 1 } { "_id" : ObjectId("..."), "a" : 200 } { "_id" : ObjectId("..."), "a" : 230 } { "_id" : ObjectId("..."), "a" : -2230 } { "_id" : ObjectId("..."), "a" : 5230 } { "_id" : ObjectId("..."), "a" : 530 } { "_id" : ObjectId("..."), "a" : 1530 }
You can use group using
> db.foo.group({ initial: { }, reduce: function(doc, acc) { if(acc.hasOwnProperty('max')) { if(acc.max < doc.a) acc.max = doc.a; } else { acc.max = doc.a } } }) [ { "max" : 5230 } ]
Since group does not have a key value, all objects are grouped into one result
Ayose source share