How to find min value in mongodb

You can use a combination of sort and limit to emulate min:

>{a: 1})
>{a: 2})
>{a: 3})
>{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 return the first document, which will be the minimum value for that field.

EDIT: note that this is written in the mongo shell, but you can do the same thing from C# or any other language using the appropriate driver methods.

Leave a Comment