find max value in collection mongodb code example
Example 1: mongodb get maximum value from collection
db.collection.aggregate([{
$group : { _id: null, max: { $max : "$age" }}
}]);
Example 2: mongodb get max value
db.collection.find().sort({age:-1}).limit(1) // for MAX
db.collection.find().sort({age:+1}).limit(1) // for MIN
Example 3: mongodb get document with maximum value from collection
var group = {$group:{_id:"$age", names:{$push:"$name"}, count:{$sum:1}}}
var sort = {$sort:{"_id":-1}}
var limit= {$limit:1}
db.selrac.aggregate([group, sort, limit])