Open In App

MongoDB – $min Operator

Last Updated : 29 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MongoDB offers a range of powerful update operators, and one of the most useful is the $min operator. This operator updates a field’s value to a specified value, but only if that value is smaller than the current field value. If the specified value is greater than or equal to the current value, no update will be performed. This behavior is particularly beneficial for scenarios where it is important to maintain minimum limits or thresholds in your data.

In this article, we will explore the $min operator in MongoDB, explain its syntax, provide detailed examples, and share best practices for using it effectively in real-world scenarios. We will also discuss the best practices for applying it in various real-world situations.

What is the $min Operator in MongoDB?

The $min operator is used to update a field with a specified value, but only if the specified value is less than the current value of that field. This means that if the new value is greater than or equal to the existing value, no update will be made.This operator is especially useful for enforcing minimum thresholds on fields, such as ensuring a rank or salary does not exceed a certain limit.

Syntax:

{ $min: { field1: value1, field2: value2 … } }

Key points:

  • It updates the field only if the specified value is less than the current value.
  • It works on both top-level fields and nested (embedded) documents using dot notation.
  • If the field does not exist, it will be created with the specified value.
  • It is used in methods such as update(), updateOne(), and others.

Examples of Using the $min Operator

To better understand how the $min operator works, let’s explore some practical examples. Imagine we have a MongoDB collection for contributors with the following structure:

  • Database: GeeksforGeeks
  • Collection: contributor
  • Document: three documents that contain the details of the contributors in the form of field-value pairs.

Sample Data

demo database and collection

Example 1: Comparing Values Using $min operator

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

Query:

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

Output:

example 1 output

Explanation: The salary of Priya is currently 5000, and since 2000 is less than 5000, the $min operator will update the salary to 2000.

Example 2: No Update When Specified Value is Greater

In this example, let’s try updating “Mohit“’s salary to 4000 using the $min operator. 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.

Query:

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

Output:

example 1 output 2

Explanation: Mohit’s current salary is 4000, which is equal to the specified value. Since the $min operator only updates the field when the specified value is less than the current value, no update occurs in this case.

Example 3: Updating Nested Documents Using $min

MongoDB allows the $min operator to work with embedded documents using dot notation. In this example, we are comparing values(or numbers) of the rank fields with the specified value. Here, the specified value is less than the current value. So, $min operator updates the value of the salary field with the help of update() method to 13.

Query:

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

Output:

example 2 output

Explanation: The current value of personal.rank is 30, which is greater than 13. So, the $min operator updates the rank to 13.

Example 4: No Update on Greater Values in Nested Documents

Now, let’s try a scenario where the current rank is lower than the specified value. So, if the current value of the rank field is greater than the specified value, then this operator will not update the value of the rank field with the specified value.

Query:

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

Output:

example 2 output 2

Explanation: The current value of personal.rank is 13, which is less than 30. Since the $min operator only updates the field if the specified value is lower, no update is made in this case.

Important Points on Mongodb $min Operator

  • The $min operator only updates a field with the specified value if the value is less than the current value of the field.
  • It supports nested documents and can be used with dot notation to update fields within embedded objects.
  • If the field does not exist, the operator creates the field and assigns the specified value.
  • This operator can be used with MongoDB update methods like update(), updateOne(), updateMany(), etc.
  • It is useful when you need to maintain a minimum value for certain fields, such as thresholds for ranks or salaries.

Conclusion

The $min operator in MongoDB is a highly effective tool for updating fields only when the new value is smaller than the existing one. It ensures that you can enforce minimum values without overwriting existing higher values. By using the $min operator, we can optimize your data updates and maintain control over your data integrity, especially when dealing with nested documents or complex data structures.

Understanding and applying the $min operator correctly can help you keep your MongoDB collections accurate, efficient, and consistent, making it a valuable addition to your MongoDB toolkit.



Next Article
Article Tags :

Similar Reads