MongoDB operations-2
-> Change age of “madhu” to 35
db.users.updateOne({name:"madhu"},{$set:{age:35}})
-> Increate salary of “madhu” by 10000
db.users.updateOne({name:"madhu"},{$inc:{salary:10000}})
-> Remove "age" field for "Madhu"
db.users.updateOne({name:"madhu"},{$unset:{age:""}})
-> Remove "city" property of"address" field for "Madhu"
db.users.updateOne({name:"madhu"},{$unset:{"address.city":""}})
-> Add "street" property of"address" field for "Madhu"
db.users.updateOne({name:"madhu"},{$set:{"address.street":"KPHB"}})
-> Add new skill “java” to user “Madhu”
db.users.updateOne({name:"madhu"},{$addToSet:{skills:"java"}} )
-> Add multiple skills to ”madhu”
db.users.updateOne({name:"madhu"},{$addToSet:{skills:{$each:["abc","xyz"]}}}) //no duplicates
db.users.updateOne({name:"madhu"},{$push:{skills:{$each:["abc","xyz”]}}})//even duplicates
-> Remove either first or last skill of ”madhu”
db.users.updateOne({name:"madhu"},{$pop:{skills:-1}}) //-1 for first & 1 for last
-> Remove skills “css” and “java” of ”madhu”
db.users.updateOne({name:"madhu"},{$pull:{skills:{$in:["css","java"]}}})
-> Add new property “designation” as “manager” to Madhu’s document
db.users.updateOne({name:"madhu"},{$set:{“designation”:”manager”}})
-> Add new property “state” as “telangana” to Madhu’s address
db.users.updateOne({name:"madhu"},{$set:{“address.state”:”telangana”}})
Check documentation for more operators:
https://www.mongodb.com/docs/v6.0/reference/operator/update/
Comments
Post a Comment