Sling Academy
Home/MongoDB/MongoDB: Using $min, $max, and $mul when updating documents

MongoDB: Using $min, $max, and $mul when updating documents

Last updated: February 01, 2024

Introduction

This tutorial aims to provide an understanding of how to use the $min, $max, and $mul operators in MongoDB when updating documents. Each of these operators can be used to modify the values of fields within your documents in different ways. MongoDB is a NoSQL database that offers flexibility in handling JSON-like documents. These operators can help you ensure data integrity and avoid overriding document fields with unwanted values.

Prerequisites

  • A basic understanding of MongoDB and its operations.
  • MongoDB installed on your local machine or access to a MongoDB database.
  • The latest version of a MongoDB driver or a MongoDB shell.

Understanding the $min Operator

The $min operator updates the value of a field to a specified value if that specified value is less than the current value of the field. If the field does not exist, the $min operator will set that field to the specified value.

// Syntax

db.collection.update(
  { <query> },
  { $min: { <field1>: <value1>, ... } }
)

// Example usage

db.products.update(
  { _id: 1 },
  { $min: { price: 100 } }
)

In the example above, if the price field in the document with _id: 1 has a value greater than 100, it will be updated to 100.

Understanding the $max Operator

The $max operator functions oppositely to the $min operator. It updates the field’s value only if the specified value is greater than the current value of the field. If the field does not exist, the $max operator will set that field to the specified value.

// Syntax

db.collection.update(
  { <query> },
  { $max: { <field1>: <value1>, ... } }
)

// Example usage

db.products.update(
  { _id: 1 },
  { $max: { price: 150 } }
)

This operator is extremely useful when wanting to preserve the highest value of a field without risking a decrease through subsequent updates.

Understanding the $mul Operator

The $mul operator is used to multiply the value of the field by the specified number. If the field does not exist, the operator will create the field and set its value to zero.

// Syntax

db.collection.update(
  { <query> },
  { $mul: { <field>: <number> } }
)

// Example usage

db.inventory.update(
  { _id: 2 },
  { $mul: { qty: 2 } }
)

In this example, the qty field will be multiplied by 2. If qty was originally set to 5, the updated value would be 10.

Additional Tips for Using $min, $max, and $mul

  • These operators can be used with the updateOne, updateMany, or bulkWrite methods.
  • They are atomic operations within single document updates, meaning the document will not be accessed by other operations during the update process.
  • It’s important to ensure that the field types are correct when using these operators, as attempting to use them on non-numeric fields could result in errors.
  • The $min and $max operators are particularly useful in financial transactions or where overriding a certain threshold is critical.
  • Remember to use write concern to guarantee that your updates are acknowledged by MongoDB.

Conclusion

Using the $min, $max, and $mul operators can help manage data efficiently and safely when performing updates in MongoDB. They ensure that you maintain data integrity while avoiding unnecessary overwriting or errors. Practice using these in your own MongoDB databases to gain familiarity and proficiency.

Next Article: Understanding $addToSet operator in MongoDB (with examples)

Previous Article: MongoDB: Using Projection in Arrays – Tutorial & Examples

Series: MongoDB Tutorials

MongoDB

You May Also Like

  • MongoDB: How to combine data from 2 collections into one
  • Hashed Indexes in MongoDB: A Practical Guide
  • Partitioning and Sharding in MongoDB: A Practical Guide (with Examples)
  • Geospatial Indexes in MongoDB: How to Speed Up Geospatial Queries
  • Understanding Partial Indexes in MongoDB
  • Exploring Sparse Indexes in MongoDB (with Examples)
  • Using Wildcard Indexes in MongoDB: An In-Depth Guide
  • Matching binary values in MongoDB: A practical guide (with examples)
  • Understanding $slice operator in MongoDB (with examples)
  • Caching in MongoDB: A practical guide (with examples)
  • CannotReuseObject Error: Attempted illegal reuse of a Mongo object in the same process space
  • How to perform cascade deletion in MongoDB (with examples)
  • MongoDB: Using $not and $nor operators to negate a query
  • MongoDB: Find SUM/MIN/MAX/AVG of each group in a collection
  • References (Manual Linking) in MongoDB: A Developer’s Guide (with Examples)
  • MongoDB: How to see all fields in a collection (with examples)
  • Type checking in MongoDB: A practical guide (with examples)
  • How to query an array of subdocuments in MongoDB (with examples)
  • MongoDB: How to compare 2 documents (with examples)