A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mongodb/mongodb-maximum-operator-max/ below:

MongoDB - $max Operator - GeeksforGeeks

MongoDB - $max Operator

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.

Key Features of the $max Operator
  1. Comparison Logic: Compares the current value of a field with the specified value and updates it only if the specified value is greater.
  2. BSON Comparison Order: Uses the BSON comparison order to evaluate data types during comparisons.
  3. Dot Notation Support: Can be used in embedded or nested documents by leveraging dot notation.
  4. Field Creation: If the specified field does not exist, the $max operator creates the field and sets its value.
  5. Versatile Usage: Supports various methods like 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 Examples

The 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:

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:

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.

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:

 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.

Important Points on $max Operator Conclusion

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