Last active
May 5, 2019 15:05
-
-
Save Abhinay-g/37cb27447243521aa9335860fb096500 to your computer and use it in GitHub Desktop.
MongoDB updating Data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>$set Operator | |
this is used to add update an existing field or add a new field | |
Sysntax : db.collection.updateOne/updateMany({--findQuery--},{$set:{fieldName:"value"}}) | |
> $inc Operator for Increment and Decrement | |
this isus ed to increment / decrement the value | |
Systax : db.collection.updateOne/updateMany({--findQuery--},{$set:{fieldName:"value"} , $inc:{age: INCREMENT_VALUE}}) | |
Note: $inc can be combined with other operator | |
IMP : You can not put $set and $inc on same field , it will throw an error ie updating and incrementing age will cause an error | |
>$min , $max , $mul | |
min is used to update field only when if specified value is less than the field value | |
max is used to update field only when if specified value is more than the field value | |
mul will multiply the field value | |
>$unset | |
this is used to delete field from a document | |
let supose we want to delete phone number from document where isSporty is true | |
db.userdata.updateMany({isSporty:true},{$unset:{phone:""}}) | |
$rename | |
this is used to rename a field | |
Syntax: db.collection.updateMany({--find--},{$rename:{fieldName:"newFieldName"}}) | |
>upsert | |
this is he third argument passed while updating a document | |
this will update document if it exist or it will create a document | |
db.userdata.update({name:"Abhinay"},{$set:{age:24,hobbies:[{title:"Yoga",frequency:2}]}},{upsert:true}) | |
this will add user Abhinay as this does not exists, | |
Importent : upsert will not only update field value but also it has power to update field name ie there is no need of $rename field | |
with upsert , as it is inserting a new document | |
>updating Array | |
the problem : with the help of elemMatch we can get the desired array,,,, but how to accesss perticular element of an array | |
Solution we use $ for that but remmeber we get only first element with $ to get specefic element we must use an identifier with $ | |
example we want to update and array where title is sports and frequency is more than 2 | |
we can get array bu find query as db.userdata.find({hobbies:{$elemMatch:{title:"Sports",frequency:{$gt:2}}}}).pretty() | |
now to access perticular field we use $ as below | |
db.userdata.updateMany({hobbies:{$elemMatch:{title:"Sports",frequency:{$gt:2}}}}, {$set:{"hobbies.$.isHighFrequency":true}}) | |
> Updating Sepcefic elemtn with $ and Identifier and condition | |
when we want to apply condition to filter result then we use an idetifier , a condition and $ | |
let suppose we found frequency greater than 3 in this case we will have multple document within array some of them satisfying condition | |
some not. now we want to filter them out who satisfy the condition for that we place an identifier then we apply condition in | |
"arrayFilters" | |
db.userdata.updateMany( | |
{"hobbies.frequency":{$gt:2}}, >> find condition | |
{$set:{"hobbies.$[el].goodFrequency":true}}, >> update with only identifier "el" | |
{arrayFilters:[{"el.frequency":{$gt:2}}]} >> condition on el | |
) | |
> updating all items with $[] | |
this is used when we want to update all the fields within an array documents | |
example increment all the frequency of document which have age greater than 30 | |
db.userdata.updateMany({"age":{$gt:30}},{$inc:{"hobbies.$[].frequency":1}}) | |
> updating first element | |
this wil only work when there is condition is on the array fields | |
db.userdata.updateMany({"age":{$gt:30}},{$inc:{"hobbies.$.frequency":1}}) >> this will throw an error where as | |
db.userdata.updateMany({"hobbies.frequency":{$gt:5}},{$set:{"hobbies.$.nota":true}}) >> this will work as ther is a condition on | |
array, by appkyting condition to array we get a palceholder so we use $ to access first matching element of an array | |
> Adding an element to an array [use $push] | |
we learned how to update an document within an array Also we added new fields to an array document. | |
Now we will learn how to add entire document to an array | |
db.userdata.updateOne({name:"Abhinay"},{$push:{hobbies:{title:"Sports",frequency:3}}}) | |
>Add multiple element $each | |
db.userdata.updateOne({name:"Abhinay"},{$push:{hobbies: | |
{$each :[{title:"Gaming",frequency:3},{title:"Running",frequency:3}]}}} | |
) | |
>Remove element from an array [$pull] | |
this will remove all occurance of an element | |
db.userdata.updateOne({name:"Abhinay"},{$pull:{hobbies:{title:"Gaming"}}}) | |
> Removing Last element of an array [$pop] | |
db.userdata.updateOne({name:"Chirs"},{$pop:{hobbies:-1}}) remove first element | |
db.userdata.updateOne({name:"Chirs"},{$pop:{hobbies:1}}) remove last element | |
> Adding Unique value to an array | |
we already have an operator for this $push then what is the difference ... ? | |
with $addToSet we can add unique element to an array | |
db.userdata.updateOne({name:"Abhinay"},{$addToSet:{hobbies:{title:"Gaming",frequency:3}}}) | |
this will add unique records to an array | |
"name" : "Abhinay", | |
"age" : 33, | |
"hobbies" : [ | |
{ | |
"title" : "Yoga", | |
"frequency" : 2 | |
}, | |
{ | |
"title" : "Yoga", | |
"frequency" : 3 | |
} | |
] | |
Above Yoga records are unique in the sense of different frequency | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment