Open In App

MongoDB – Field Update Operators

Last Updated : 28 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

MongoDB offers a range of powerful field update operators that enable efficient modification of specific fields within documents. These operators allow developers to update specific fields in documents without rewriting the entire document, thus improving performance and operational efficiency.

By guaranteeing atomicity, MongoDB ensures that each update operation is fully completed, preserving data consistency and integrity. In this article, We will learn about the several Field Update Operators in MongoDB with the help of examples and so on.

What are Field Update Operators in MongoDB?

Field update operators in MongoDB are special operators used to modify the values of fields in existing documents. These operators ensure atomic updates, meaning the changes are applied in a single, indivisible operation. This guarantees data consistency and integrity while simplifying database operations.

Benefits of Field Update Operators:

  • Efficiency: Update specific fields without rewriting entire documents.
  • Atomicity: Ensures data integrity by completing operations in full.
  • Flexibility: Modify multiple fields simultaneously or apply operations to nested fields.

Common Field Update Operators in MongoDB

MongoDB provides different types of field update operators to update the values of the fields of the documents that match the specified condition. The following table contains the field update operators:

Operator Description
$currentDate Sets the value of a field to a current date, either as a Date or a Timestamp.
$inc Increments the value of the field by the specified amount.
$min Updates the field only if the specified value is less than the existing field value
$max Updates the field if the specified value is greater than the existing field value.
$mul Multiplies the value of the field by the specified amount.
$rename Renames a field.
$setOnInsert Sets the value of a field if an update results in an insert of a document. It has no effect on update operations that modify existing documents.

Examples of MongoDB Field Update Operators

In the following examples, we use a database named GeeksforGeeks and a collection named Employee. The collection contains documents representing employee details.

  • Database: GeeksforGeeks 
  • Collection: Employee 
  • Document: two documents that contain the details of the employees in the form of field-value pairs.

Example 1: Updating a Field with $currentDate

In the example, We want to update the joiningDate field of an employee document where the first name is “Om” to the current date.

Query:

db.Employee.updateOne({"name.first": "Om"}, 
{$currentDate: {joiningDate: true}})

Output:

Example 2: Incrementing a field’s value using $inc

In this example, We want to increment the salary field by 3000 for an employee whose first name is “Sumit” using $inc Operator in MongoDB.

Query:

db.Employee.update({"name.first": "Sumit"}, 
{$inc: {"personalDetails.salary": 3000}})

Output:

Example 3: Updating a field if the new value is greater using $max

In this example, we are comparing values(or numbers) of the salary fields with the specified value, i.e., 40000. 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 40000. 

Query:

db.Employee.update({"name.first": "Sumit"}, 
{$max: {"personalDetails.salary": 40000}})

Output:

Example 4: Updating a Field if the New Value is Smaller Using $min

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 less than the current value. So. $min operator updates the value of the salary field with the help of update() method to 5000. 

Query:

db.Employee.update({"name.first": "Sumit"}, 
{$min: {"personalDetails.salary": 5000}})

Output:

Example 5: Multiplying a field’s value using $mul

In this example, We want to double the salary field for an employee whose first name is “Sumit“.

Query:

db.Employee.update({"name.first": "Sumit"}, 
{$mul: {"personalDetails.salary": 2}})

Output:

Example 6: Renaming a field using $rename

In this example, we are renaming the name of department field to unit in the employee’s document whose first name is Om. 

Query:

db.Employee.update({"name.first": "Om"}, 
{$rename: {"department": "unit"}})

Output:

Example 7: Inserting new fields using $setOnInsert

In this example, we are creating new document in Employee collection with the help of update() method by setting the value of upsert field to true and using $setOneInsert operator assign the values to department and salary fields in the document. 

Query:

db.Example.update({name: {first: "Mona", last: "Singh"}, 
personalDetails: {age: 24, contactInfo: 4578934201}},
{$setOnInsert: {department: "HR", salary: 30000}},
{upsert: true})

Output:

-setOnInsert

Conclusion

Field update operators in MongoDB provide an efficient and flexible way to modify data in documents. By using operators like $currentDate, $inc, $min, $max, $mul, $rename, and $setOnInsert, developers can make precise updates that enhance the performance and simplify database operations. These atomic operations ensure data consistency and integrity, making them essential for robust MongoDB applications. Learning these operators can significantly improve your database management skills.

FAQs

Can I use multiple field update operators in a single update operation?

Yes, MongoDB allows the use of multiple field update operators in a single operation, letting you perform various operations on the same or different fields in a document simultaneously.

Are field update operations in MongoDB atomic?

Yes, field update operations in MongoDB are atomic at the document level. This means each update operation is completed in full or not applied at all, ensuring data consistency.

Can I update nested fields using field update operators? Y

Yes, you can update nested fields in a MongoDB document by using dot notation to specify the path to the nested field (e.g., "address.city": "New York").



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: