$min
The $min operator returns the minimum value from an array of values. It can be used in aggregation stages to find the minimum value for a specified field across multiple documents.
Parameters
-
expression: The expression to evaluate. This can be a field path, a variable, or any expression that resolves to a value.
Example (MongoDB Shell)
The following example demonstrates the usage of the $min operator to find the minimum value of the age field across multiple documents.
Create sample documents
db.users.insertMany([ { name: "John", age: 35 }, { name: "Jane", age: 28 }, { name: "Bob", age: 42 }, { name: "Alice", age: 31 } ]);
Query example
db.users.aggregate([ { $group: { _id: null, minAge: { $min: "$age" } } }, { $project: { _id: 0, minAge: 1 } } ])
Output
[ { minAge: 28 } ]
Code examples
To view a code example for using the $min command, choose the tab for the language that you want to use: