- Reference >
- Operators >
- Aggregation Framework Operators >
- Group Aggregation Operators >
- $min (aggregation)
$min (aggregation)¶
-
$min¶ The
$minoperator returns the lowest non-null value of a field in the documents for a$groupoperation.Changed in version 2.4: If some, but not all, documents for the
$minoperation have either anullvalue for the field or are missing the field, the$minoperator only considers the non-null and the non-missing values for the field. If all documents for the$minoperation havenullvalue for the field or are missing the field, the$minoperator returnsnullfor the minimum value.Before 2.4, if any of the documents for the
$minoperation were missing the field, the$minoperator would not return any value. If any of the documents for the$minhad the valuenull, the$minoperator would return anull.Example
The
userscollection contains the following documents:To find the minimum value of the
agefield from all the documents, use the$minoperator:The operation returns the value of the
agefield in theminAgefield:To find the minimum value of the
agefield for only those documents with_idstarting with the lettera, use the$minoperator after a$matchoperation:The operation returns the minimum value of the
agefield for the two documents with_idstarting with the lettera:
Example
The
userscollection contains the following documents where some of the documents are either missing theagefield or theagefield containsnull:The following operation finds the minimum value of the
agefield in all the documents:Because only some documents for the
$minoperation are missing theagefield or haveagefield equal tonull,$minonly 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
agefield for only those documents where the_idequals"xxx001"or"zzz001":The
$minoperation returnsnullfor the minimum age since all documents for the$minoperation havenullvalue for the fieldageor are missing the field: