MongoDB Field Update Operator - $set
Description
In MongoDB, the $set operator is used to replace the value of a field to the specified value. If the given field does not exist in the document, the $set operator will add the field to the specified value.
Syntax:
{ $set: { <field1>: <value1>, ... } }
Parameters:
Name | Description |
---|---|
field1 | the field which value going to be changed |
value1 | the new value for the field |
Our database name is 'myinfo' and our collection name is "employee". Here, is the collection bellow.
Sample collection "employee"
{
"_id" : 1,
"emp_id" : 1231,
"pinfo" : {
"empfname" : "Alen",
"emplname" : "Jones"
},
"phno" : "123-123-1234",
"email" : "[email protected]",
"sector" : 2,
"offinfo" : [
"manager",
"sales"
]
}
{
"_id" : 2,
"emp_id" : 1232,
"pinfo" : {
"empfname" : "Robart",
"emplname" : "Bolt"
},
"phno" : "123-123-1235",
"email" : "[email protected]",
"sector" : 3,
"offinfo" : [
"administration",
"MD"
]
}
Example of MongoDB $set operator to update first matching document
If we want to change the content of email field for that employee which id is 1231 the following mongodb statement can be used:
>db.employeeset.update( { emp_id: 1231 },{ $set: {"email": "[email protected]"}});
To see the updated output -
> db.employee.find().pretty();
N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.
Output of the command
{ "_id" : 1, "email" : "[email protected]", "emp_id" : 1231, "offinfo" : [ "manager", "sales" ], "phno" : "123-123-1234", "pinfo" : { "empfname" : "Alen", "emplname" : "Jones" }, "sector" : 2 } { "_id" : 2, "emp_id" : 1232, "pinfo" : { "empfname" : "Robart", "emplname" : "Bolt" }, "phno" : "123-123-1235", "email" : "[email protected]", "sector" : 3, "offinfo" : [ "administration", "MD" ] }
Example of MongoDB $set operator to update all matching document
If we want to update the value of "block" with "E" for all those documents which matching the condition sector greater than equal to 2, the following mongodb queries can be used -
>db.employeeset.update( { "sector":{$gte:2} },{ $set: {"block": "E"}},{multi : true});
Here in the above example "multi : true" have used to effect the updates for all the matching document, and the important thing to be noted that, the "block" field does not exists in the document, so, a new field "block" have added with value "E" in the document.
To see the updated output -
> db.employee.find().pretty();
N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.
Output of the command:
{ "_id" : 2, "block" : "E", "email" : "[email protected]", "emp_id" : 1232, "offinfo" : [ "administration", "MD" ], "phno" : "123-123-1235", "pinfo" : { "empfname" : "Robart", "emplname" : "Bolt" }, "sector" : 3 } { "_id" : 1, "block" : "E", "email" : "[email protected]", "emp_id" : 1231, "offinfo" : [ "manager", "sales" ], "phno" : "123-123-1234", "pinfo" : { "empfname" : "Alen", "emplname" : "Jones" }, "sector" : 2 }
Previous:
$rename
Next:
$setOnInsert
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/mongodb/mongodb-field-update-operator-$set.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics