how to update document in mongodb


How to update document in mongodb

update() is use to update existing document data in Mongodb and it will work with $set to update new data.

Syntax

db.COLLECTION_NAME.update(old_data, new_data)

Example

db.stud.save(
{
 "roll":101,
 "name":"Amar Kumar",
 "addr":"Hyderabad"   
}
)
{
    "_id" : ObjectId("56e3e68424f2c2611ca9c290"),
    "roll" : 101,
    "name" : "Amar Kumar",
    "addr" : "Hyderabad"
}

Now Update Record Amar Kumar to Vikash Kumar

db.stud.update(
{
    "name":"Amar Kumar"},{$set:{"name":"Vikash Kumar"}}
)
{
    "_id" : ObjectId("56e3e68424f2c2611ca9c290"),
    "roll" : 101,
    "name" : "Vikash Kumar",
    "addr" : "Hyderabad"
}

Please see above Amar Kumar is updated with Vikash Kumar






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229