- Reference >
- Operators >
- Update Operators >
- Field Update Operators >
- $max
$max¶
On this page
Definition¶
-
$max¶ The
$maxoperator updates the value of the field to a specified value if the specified value is greater than the current value of the field. The$maxoperator can compare values of different types, using the BSON comparison order.The
$maxoperator expression has the form:To specify a
<field>in an embedded document or in an array, use dot notation.
Examples¶
Use $max to Compare Numbers¶
Consider the following document in the collection scores:
The highScore for the document currently has the value
800. The following operation uses $max to compare
the 800 and the specified value 950 and updates the value
of highScore to 950 since 950 is greater than 800:
The scores collection now contains the following modified document:
The next operation has no effect since the current value of the
field highScore, i.e. 950, is greater than 870:
The document remains unchanged in the scores collection:
Use $max to Compare Dates¶
Consider the following document in the collection tags:
The following operation compares the current value of the
dateExpired field, i.e.
ISODate("2013-10-01T16:38:16.163Z"), with the specified date
new Date("2013-09-30") to determine whether to update the
field:
The operation does not update the dateExpired field: