MongoDB – Update Document in a Collection
In MongoDB, we have two ways to update a document in a collection. 1) update() method 2) save() method. Although both the methods update an existing document, they are being used in different scenarios. The update() method is used when
we need to update the values of an existing document while save() method is used to replace the existing document with the document that has been passed in it.
To update a document in MongoDB, we provide a criteria in command and the document that matches that criteria is updated.
Updating Document using update() method
Syntax :
db.collection_name.update(criteria, update_data)
Example:
For example: Lets say I have a collection named “department” in the database employeeDB. The documents inside “department” are:
db.department.find().pretty()
{
"_id" : ObjectId("5b3725025eb29c373ddadd51"),
"dept_id" : 4,
"name" : "Sales"
}
{
"_id" : ObjectId("5b3725145eb29c373ddadd52"),
"dept_id" : 3,
"name" : "Production"
}
{
"_id" : ObjectId("5b3727195eb29c373ddadd56"),
"dept_id" : 2,
"name" : "Marketing"
}
Now suppose if I want to update the name of ‘Marketing’ with the name “Finance”. The command for this would be:
db.department.update({“name”:”Marketing”},{$set:{“name”:”Finance”}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.department.find().pretty()
{
"_id" : ObjectId("5b3725025eb29c373ddadd51"),
"dept_id" : 4,
"name" : "Sales"
}
{
"_id" : ObjectId("5b3725145eb29c373ddadd52"),
"dept_id" : 3,
"name" : "Production"
}
{
"_id" : ObjectId("5b3727195eb29c373ddadd56"),
"dept_id" : 2,
"name" : "Finance"
}
Note : By default the update method updates a single document. In the above example we had only one document matching with the criteria, however if there were more then also only one document would have been updated. To
enable update() method to update multiple documents you have to set “multi” parameter of this method to true as shown below.
update multiple documents with the update() method:
db.department.update({"name":"Production"},
{$set:{"name":"Finance"}},{multi:true})
Updating Document using save() method
Syntax:
db.collection_name.save( {_id:ObjectId(), new_document} )
Lets take the same example that we have seen above. Now we want to update the name of “Finance” to “Marketing”. To work with save() method you should know the unique _id field of that document.
Note : A very important point to note is that when you do not provide the _id field while using save() method, it calls insert() method and the passed document is inserted into the collection
as a new document
To get the _id of a document, you can either type this command:
db.debartment.find().pretty()
Now we know the unique _id field of that document. Lets write the command using save() method.
db.department.find()
{ "_id" : ObjectId("5b3725025eb29c373ddadd51"), "dept_id" : 4, "name" : "Sales" }
{ "_id" : ObjectId("5b3725145eb29c373ddadd52"), "dept_id" : 3, "name" : "Production" }
{ "_id" : ObjectId("5b3727195eb29c373ddadd56"), "dept_id" : 2, "name" : "Finance" }
db.department.save({“_id” : ObjectId(“5b3725025eb29c373ddadd51”), “name”:”Testing”})
OutPut :
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.department.find()
{ "_id" : ObjectId("5b3725025eb29c373ddadd51"), "name" : "Testing" }
{ "_id" : ObjectId("5b3725145eb29c373ddadd52"), "dept_id" : 3, "name" : "Production" }
{ "_id" : ObjectId("5b3727195eb29c373ddadd56"), "dept_id" : 2, "name" : "Finance" }