Docs Menu
Docs Home
MongoDB Manual
/ / / /


On this page

  • Definition
  • Behavior
  • Examples

The $max operator updates the value of the field to a specified value if the specified value is greater than the current value of the field. The $max operator can compare values of different types, using the BSON comparison order.

The $max operator expression has the form:

{ $max: { <field1>: <value1>, ... } }

To specify a <field> in an embedded document or in an array, use dot notation.

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 exists, the $max operator sets the field to the specified value.

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $max 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).

Create the scores collection:

{ _id: 1, highScore: 800, lowScore: 200 }

The highScore for the document currently has the value 800. The following operation:

  • Compares the highScore, 800, to the specified value, 950

  • Updates highScore to 950 since 950 is greater than 800

db.scores.update( { _id: 1 }, { $max: { highScore: 950 } } )

The scores collection now contains the following modified document:

{ _id: 1, highScore: 950, lowScore: 200 }

The next operation has no effect since the value of highScore, 950, is greater than 870:

db.scores.updateOne( { _id: 1 }, { $max: { highScore: 870 } } )

The document remains unchanged in the scores collection:

{ _id: 1, highScore: 950, lowScore: 200 }

Create the tags collection:

_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16.163Z")

The following operation compares the current value of the dateExpired field, ISODate("2013-10-01T16:38:16.163Z"), with the specified date new Date("2013-09-30") to determine whether to update the field:

{ _id: 1 },
{ $max: { dateExpired: new Date("2013-09-30") } }

new Date("2013-09-30") is not the newest date, so the operation does not update the dateExpired field:

_id: 1,
desc: "decorative arts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16.163Z")


See also: