![]() |
VOOZH | about |
MongoDB field update operators modify specific fields in documents without replacing the entire document, enabling efficient and atomic updates.
They support precise, in-place modifications during update operations.
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:
In the following examples, we use a database named GeeksforGeeks and a collection named Employee.
Sets the value of a field to a current date, either as a Date or a Timestamp.
Example: Update the joiningDate field of an employee document where the first name is "Alex" to the current date.
db.Employee.updateOne(
{ "name.first": "Alex" },
{ $currentDate: { joiningDate: true } }
)
Output:
Increments the value of the field by the specified amount.
Example: Increment the salary field by 3000 for an employee whose first name is "Daniel" using $inc Operator in MongoDB.
db.Employee.updateOne(
{ "name.first": "Daniel" },
{ $inc: { "personalDetails.salary": 3000 } }
)
Output:
Updates the field if the specified value is greater than the existing field value.
Example: 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 updateOne() method to 40000.
db.Employee.updateOne(
{ "name.first": "Daniel" },
{ $max: { "personalDetails.salary": 40000 } }
)
Output:
Updates the field only if the specified value is less than the existing field value
Example: 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 updateOne() method to 5000.
db.Employee.updateOne(
{ "name.first": "Daniel" },
{ $min: { "personalDetails.salary": 5000 } }
)
Output:
Multiplies the value of the field by the specified amount.
Example: Double the salary field for an employee whose first name is "Daniel".
db.Employee.updateOne(
{ "name.first": "Daniel" },
{ $mul: { "personalDetails.salary": 2 } }
)
Output:
This operator is used to rename a field.
Example: Renaming the name of department field to unit in the employee’s document whose first name is Alex.
db.Employee.updateOne(
{ "name.first": "Alex" },
{ $rename: { unit: "department" } }
)
Output:
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.
Example: Creating new document in Employee collection with the help of updateOne() method by setting the value of upsert field to true and using $setOnInsert operator assign the values to department and salary fields in the document.
db.Employee.updateOne(
{
name: { first: "Maria", last: "Will" },
personalDetails: { age: 24, contactInfo: 4578934201 }
},
{
$setOnInsert: { department: "HR", salary: 30000 }
},
{ upsert: true }
)
Output: