MongoDB provides different types of field update operators to update the values of the fields of the documents that matches the specified condition. The following table contains the field update operators:
|$currentDate||This operator is used to set the value of a field to current date, either as a Date or a Timestamp.|
|$inc||This operator is used to increment the value of the field by the specified amount.|
|$min||This operator is used only to update the field if the specified value is less than the existing field value|
|$max||This operator is used only to update the field if the specified value is greater than the existing field value.|
|$mul||This operator is used to multiply the value of the field by the specified amount.|
|$rename||This operator is used to rename a field.|
|$setOnInsert||This operator is used to set 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.|
In the following examples, we are working with:
Document: two documents that contain the details of the employees in the form of field-value pairs.
Updating the value of date field using
In the example, we are updating the value of joiningDate field of an employee’s document whose first name is Om.
Increment the value of the field using
In this example, we are updating the salary field of an employee’s document whose name is Sumit.
Comparing values (or numbers) using
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.
Comparing values (or numbers) 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 less than the current value. So.
$min operator updates the value of the salary field with the help of update() method to 5000.
Multiplying the value of a field using
In this example, we are multiplying the value of salary field by 2 in the document who matches the specified condition, i.e., name = Sumit.
Renaming a field using
In this example, we are renaming the name of experienceYear field to experience in the employee’s document whose first name is Om.
Inserting new fields in new documents using
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.
- MongoDB - Update Single Document Using MongoShell
- MongoDB - Update Multiple Documents Using MongoShell
- MongoDB Bitwise Update Operator
- MongoDB - Update() Method
- How to update single and multiple documents in MongoDB using Node.js ?
- MongoDB - Comparison Query Operators
- MongoDB - Logical Query Operators
- Mongoose | update() Function
- MongoDB : An introduction
- MongoDB: Getting Started
- MongoDB and Python
- Nodejs - Connect MongoDB with Node app using MongooseJS
- MongoDB vs MySQL
- Difference between Oracle and MongoDB
- Signup Form Using Nodejs and MongoDB
- Introduction about Node.js and MongoDB
- MongoDB | Create Database using MongoShell
- MongoDB | Delete Database using MongoShell
- Defining, Creating and Dropping a MongoDB collection
- Difference between RDBMS and MongoDB
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.