- Reference >
- Operators >
- Aggregation Framework Operators >
- Group Aggregation Operators >
- $min (aggregation)
$min (aggregation)¶
-
$min
¶ The
$min
operator returns the lowest non-null value of a field in the documents for a$group
operation.Changed in version 2.4: If some, but not all, documents for the
$min
operation have either anull
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 havenull
value for the field or are missing the field, the$min
operator returnsnull
for the minimum value.Before 2.4, if any of the documents for the
$min
operation were missing the field, the$min
operator would not return any value. If any of the documents for the$min
had the valuenull
, the$min
operator would return anull
.Example
The
users
collection contains the following documents:To find the minimum value of the
age
field from all the documents, use the$min
operator:The operation returns the value of the
age
field in theminAge
field:To find the minimum value of the
age
field for only those documents with_id
starting with the lettera
, use the$min
operator after a$match
operation:The operation returns the minimum value of the
age
field for the two documents with_id
starting with the lettera
:
Example
The
users
collection contains the following documents where some of the documents are either missing theage
field or theage
field containsnull
:The following operation finds the minimum value of the
age
field in all the documents:Because only some documents for the
$min
operation are missing theage
field or haveage
field equal tonull
,$min
only considers the non-null and the non-missing values and the operation returns the following document:The following operation finds the minimum value of the
age
field for only those documents where the_id
equals"xxx001"
or"zzz001"
:The
$min
operation returnsnull
for the minimum age since all documents for the$min
operation havenull
value for the fieldage
or are missing the field: