MongoDB Update Document

MongoDB provides db.myCollection.update() method to update or modify document. Also you can use db.myCollection.save() method to upsert document. Means update() and save() methods use to update/modify document.


Syntax of update() method


The syntax of the update() method is given below.


db.Collection_Name.update(Selecttion_Criteria,Updated_Date)



Example


Suppose we have a collection of EMP as like below.


/* 1 */
{
    "_id" : ObjectId("584a7c33309356d26bef176e"),
    "Name" : "Dilip",
    "Salary" : 50000.0,
    "Project" : "Codeafri Mongodb",
    "DOJ" : ISODate("2014-12-01T20:19:55.782Z")
}

/* 2 */
{
    "_id" : ObjectId("584a7c33309356d26bef176f"),
    "Name" : "Vipul",
    "Salary" : 30000.0,
    "Project" : "Codeafri UI",
    "DOJ" : ISODate("2015-11-01T20:19:55.782Z")
}

/* 3 */
{
    "_id" : ObjectId("584a7c33309356d26bef1770"),
    "Name" : "Ashish",
    "Salary" : 100000.0,
    "Project" : "Codeafri Project architect",
    "DOJ" : ISODate("2013-10-01T20:19:55.782Z")
}


Suppose we want to modify Salary of Dilip from 50000 to 70000, now we can update salary executing the following command.


>db.EMP.update({'Name':'Dilip'},{$set:{'Salary':70000}})



Find Updated Records



>db.EMP.find()



Output



/* 1 */
{
    "_id" : ObjectId("584a7c33309356d26bef176e"),
    "Name" : "Dilip",
    "Salary" : 70000.0,
    "Project" : "Codeafri Mongodb",
    "DOJ" : ISODate("2014-12-01T20:19:55.782Z")
}

/* 2 */
{
    "_id" : ObjectId("584a7c33309356d26bef176f"),
    "Name" : "Vipul",
    "Salary" : 30000.0,
    "Project" : "Codeafri UI",
    "DOJ" : ISODate("2015-11-01T20:19:55.782Z")
}

/* 3 */
{
    "_id" : ObjectId("584a7c33309356d26bef1770"),
    "Name" : "Ashish",
    "Salary" : 100000.0,
    "Project" : "Codeafri Project architect",
    "DOJ" : ISODate("2013-10-01T20:19:55.782Z")
}



You can see in output (bold) Salary of Dilip has been updated with 70000.


Syntax of save() method


db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})



Save() methods replaces the existing document with a new document.

Example

Suppose we have an employee collection EMP as below.


/* 1 */
{
    "_id" : ObjectId("584a7ec1309356d26bef1771"),
    "Name" : "Dilip",
    "Salary" : 50000.0,
    "Project" : "Codeafri Mongodb",
    "DOJ" : ISODate("2014-12-01T20:19:55.782Z")
}

/* 2 */
{
    "_id" : ObjectId("584a7ec1309356d26bef1772"),
    "Name" : "Vipul",
    "Salary" : 30000.0,
    "Project" : "Codeafri UI",
    "DOJ" : ISODate("2015-11-01T20:19:55.782Z")
}

/* 3 */
{
    "_id" : ObjectId("584a7ec1309356d26bef1773"),
    "Name" : "Ashish",
    "Salary" : 100000.0,
    "Project" : "Codeafri Project architect",
    "DOJ" : ISODate("2013-10-01T20:19:55.782Z")
}


Due to some reason we want to replace Dilip’s document with another document. Here we can use save() method, please see the following command


db.EMP.save(
   {
             "_id" : ObjectId("584a7ec1309356d26bef1771"),
             "Name":"Dilip Kumar Singh",
             "Salary":90000,
             "Project":"Codeafri Mongodb, Mongodb atlas",
             "DOJ":ISODate("2016-12-01T20:19:55.782Z")
             }
)




Find updated records



db.EMP.find()



Output



/* 1 */
{
    "_id" : ObjectId("584a7ec1309356d26bef1772"),
    "Name" : "Vipul",
    "Salary" : 30000.0,
    "Project" : "Codeafri UI",
    "DOJ" : ISODate("2015-11-01T20:19:55.782Z")
}

/* 2 */
{
    "_id" : ObjectId("584a7ec1309356d26bef1773"),
    "Name" : "Ashish",
    "Salary" : 100000.0,
    "Project" : "Codeafri Project architect",
    "DOJ" : ISODate("2013-10-01T20:19:55.782Z")
}

/* 3 */
{
    "_id" : ObjectId("584a7ec1309356d26bef1771"),
    "Name" : "Dilip Kumar Singh",
    "Salary" : 90000.0,
    "Project" : "Codeafri Mongodb, Mongodb atlas",
    "DOJ" : ISODate("2016-12-01T20:19:55.782Z")
}



You can see the bold document in output which is a newly replaced document.