- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $min (aggregation)
$min (aggregation)¶
On this page
Definition¶
-
$min
¶ Returns the minimum value.
$min
compares both value and type, using the specified BSON comparison order for values of different types.$min
is available in the following stages:$group
$project
$addFields
(Available starting in MongoDB 3.4)$set
(Available starting in MongoDB 4.2)$replaceRoot
(Available starting in MongoDB 3.4)$replaceWith
(Available starting in MongoDB 4.2)$match
stage that includes an$expr
expression
In MongoDB 3.2 and earlier,
$min
is available in the$group
stage only.When used in the
$group
stage,$min
has the following syntax and returns the minimum value that results from applying an expression to each document in a group of documents that share the same group by key:When used in the other supported stages,
$min
returns the minimum of the specified expression or list of expressions for each document and has one of two syntaxes:$min
has one specified expression as its operand:$min
has a list of specified expressions as its operand:
For more information on expressions, see Expressions.
Behavior¶
Null or Missing Values¶
If some, but not all, documents for the $min
operation have either a null
value for the field or are
missing the field, the $min
operator only considers the
non-null and the non-missing values for the field.
If all documents for the $min
operation have null
value for the field or are missing the field, the $min
operator returns null
for the minimum value.
Array Operand¶
In the $group
stage, if the expression resolves to an
array, $min
does not traverse the array and compares the array
as a whole.
In the other supported stages:
- With a single expression as its operand, if the expression resolves
to an array,
$min
traverses into the array to operate on the numerical elements of the array to return a single value. - With a list of expressions as its operand, if any of the expressions
resolves to an array,
$min
does not traverse into the array but instead treats the array as a non-numerical value.
Examples¶
Use in $group
Stage¶
Consider a sales
collection with the following documents:
Grouping the documents by the item
field, the following operation
uses the $min
accumulator to compute the minimum amount and
minimum quantity for each grouping.
The operation returns the following results:
Use in $project
Stage¶
A collection students
contains the following documents:
The following example uses the $min
in the
$project
stage to calculate the minimum quiz scores, the
minimum lab scores, and the minimum of the final and the midterm:
The operation results in the following documents:
In the other supported stages:
- With a single expression as its operand, if the expression resolves
to an array,
$min
traverses into the array to operate on the numerical elements of the array to return a single value. - With a list of expressions as its operand, if any of the expressions
resolves to an array,
$min
does not traverse into the array but instead treats the array as a non-numerical value.