How to find min value in mongodb
问题 How do you do the equivalent of SELECT MIN(Id) AS MinId FROM Table with MongoDB? It looks like I will have to use MapReduce but I can't find any example that shows how to do this. 回答1: You can use a combination of sort and limit to emulate min : > db.foo.insert({a: 1}) > db.foo.insert({a: 2}) > db.foo.insert({a: 3}) > db.foo.find().sort({a: 1}).limit(1) { "_id" : ObjectId("4df8d4a5957c623adae2ab7e"), "a" : 1 } sort({a: 1}) is an ascending (minimum-first) sort on the a field, and we then only