MongoDB – Field Update Operators

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:

Operator Description
$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:

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

Updating the value of date field using $currentDate operator:

In the example, we are updating the value of joiningDate field of an employee’s document whose first name is Om.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Increment the value of the field using $inc operator:

In this example, we are updating the salary field of an employee’s document whose name is Sumit.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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., 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Multiplying the value of a field using $mul operator:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Renaming a field using $rename operator:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Inserting new fields in new documents 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.