- Reference >
- Operators >
- Update Operators >
- Field Update Operators >
- $min
$min¶
On this page
Definition¶
-
$min
¶ The
$min
updates the value of the field to a specified value if the specified value is less than the current value of the field. The$min
operator can compare values of different types, using the BSON comparison order.To specify a
<field>
in an embedded document or in an array, use dot notation.
Behavior¶
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.
If the field does not exist, the $min
operator sets the
field to the specified value.
For comparisons between values of different types, such as a number and
a null, $min
uses the BSON comparison order.
Starting in MongoDB 5.0, mongod
no longer raises an
error when you use an update operator like $min
with an empty operand expression ( { }
). An empty update results
in no changes and no oplog entry is created (meaning that the
operation is a no-op).
Examples¶
Use $min
to Compare Numbers¶
Create the scores
collection:
The lowScore
for the document currently has the value
200
. The following operation uses $min
to compare
200
to the specified value 150
and updates the value of
lowScore
to 150
since 150
is less than 200
:
The scores
collection now contains the following modified document:
The next operation has no effect since the current value of the
field lowScore
, i.e 150
, is less than 250
:
The document remains unchanged in the scores
collection:
Use $min
to Compare Dates¶
Create the tags
collection:
The following operation compares the current value of the
dateEntered
field, i.e. ISODate("2013-10-01T05:00:00Z")
,
with the specified date new Date("2013-09-25")
to determine
whether to update the field:
The operation updates the dateEntered
field: