Open In App

MongoDB – $max Operator

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB $max or maximum operator is one of the field update operators. $max operator updates the field with the specified value if the specified value is greater than the current value.

Important Points

  • This operator will compare the values of different data types according to the BSON comparison order.
  • You can also use this operator in embedded/nested documents using dot notation.
  • You can use this operator in methods like update(), updateOne(), exist, etc. according to your requirements.
  • If the given field does not exist, then this operator will create a field and set the value of that field.

Syntax

{ $max: { field1: value1, field2: value2 ... } }

MongoDB $max Operator Examples

In the following examples, we are working with:

Database: GeeksforGeeks

Collection: contributor

Document: three documents that contain the details of the contributors in the form of field-value pairs.

demo database and collection

Example 1: Comparing values (or numbers) using $max operator

In this example, we are comparing values(or numbers) of the salary fields with the specified value, i.e., 5000. Here, the specified value is greater than the current value. So, $max operator updates the value of the salary field with the help of update() method to 5000.

Query:

db.contributor.update({name: "Mohit"}, {$max: {salary: 5000}})

Output:

example 1 outputIf the current value of the salary field is greater than the specified value, then this operator will not update the value of the salary field with the specified value, i.e., 4000. 

Query:

db.contributor.update({name: "Mohit"}, {$max: {salary: 4000}})

Output:

example 1 output 2

Example 2: Comparing values (or numbers) in nested documents using $max operator

In this example, we are comparing values(or numbers) of the rank fields with the specified value, i.e., 30. Here, the specified value is greater than the current value. So, $max operator updates the value of the salary field with the help of update() method to 30. 

Query:

db.contributor.update({name: "Priya"}, {$max: {"personal.rank": 30}})

Output:

example 2 output If the current value of the rank field is less than the specified value, then this operator will not update the value of the rank field with the specified value, i.e., 13. 

Query:

db.contributor.update({name: "Priya"}, {$max: {"personal.rank": 13}})

Output:

example 2 output 2

Key Takeaways About $max Operator

  • The $max operator in MongoDB is used to select documents where the value of a field is greater than or equal to the specified value
  • It is a comparison operator that can be used in queries with find(), update(), and other methods.
  • It is commonly used to filter documents based on values that are greater than or equal to a specified threshold.
  • MongoDB supports limited cross-BSON comparison through Type Bracketing when using the $max operator.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads