Last Updated : 12 Jul, 2025
The $max
operator in MongoDB is one of the field update operators used to conditionally update fields within a document. It updates a field only if the specified value is greater than the current value, making it highly efficient for managing thresholds and ensuring data accuracy.
This operator is versatile, supporting updates in nested documents using dot notation and creating fields if they do not already exist. By using BSON comparison rules, $max
ensures precise updates across various data types, making it a critical tool for developers working with dynamic and structured datasets.
$max
operator creates the field and sets its value.update()
, updateOne()
, and updateMany()
.Syntax
{ $max: { field1: value1, field2: value2 ... } }
This syntax allows you to specify one or more fields and their corresponding values to update.
MongoDB $max Operator ExamplesThe following examples demonstrate how to use the $max
operator in MongoDB to update fields conditionally based on the specified value. We will explore various scenarios, including updating simple fields and nested fields in documents.
In the following examples, we are using:
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:
If 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:
Explanation: In this case, if the current value of salary
is greater than 4000 (e.g., 5000), the field will not be updated.
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:
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:
Explanation: If the current value of personal.rank
is greater than 13 (e.g., 30), the field will not be updated.
$max
operator in MongoDB is used to select documents where the value of a field is greater than or equal to the specified value.find()
, update()
, and other methods.$max
operator.The $max
operator in MongoDB is a powerful tool for conditional updates, ensuring fields are only updated when the specified value exceeds the current one. By supporting BSON comparisons, nested document updates, and versatile methods, it enhances the flexibility and performance of database operations. Incorporating $max
effectively can streamline our MongoDB workflows while maintaining data consistency.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4