Home >>MongoDB Tutorial >MongoDB Update Document

MongoDB Update Document

How to Update Document(Data) using MongoDB

To update a document into a list, MongoDB's update() and save() methods are used. The update() method updates the existing document values, while the save() method replaces the existing document with the document passed by the save() method.

MongoDB Update() Method

The update() method updates the values in the existing document.

Syntax

The basic syntax of update() method is as follows −

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Example

Consider the mycol collection has the following data.

{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"phptpoint Overv"}

The new title 'New MongoDB Tutorial' for the documents with the title 'MongoDB Overview' is set in the following example.

> db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"phptpoint Overview"}
>

MongoDB can update only a single document by default. You need to set the parameter 'multi' to true in order to update several documents.

> db.mycol.update({'title':'MongoDB Overview'},
   {$set:{'title':'New MongoDB Tutorial'}},{multi:true})

MongoDB Save() Method

The save() method replaces the new document passed in the save() method for the existing document.

Syntax

The basic MongoDB save() method syntax is shown below –

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

Example

Following example will replace the document with the _id '5983548781331adf45ec5'.

>db.mycol.save(
   {
      "_id" : ObjectId("507f191e810c19729de860ea"), 
		"title":"phptpoint New Topic",
      "by":"phptpoint"
   }
)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("507f191e810c19729de860ea")
})
>db.mycol.find()
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"phptpoint New Topic",
   "by":"phptpoint"}
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"NoSQL Overview"}
{ "_id" : ObjectId("507f191e810c19729de860e6"), "title":"phptpoint Overview"}
>

MongoDB findOneAndUpdate() method

The findOneAndUpdate() method updates the values in the existing document.

Syntax

The basic syntax of findOneAndUpdate() method is as follows −

> db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example

Suppose we created a collection called empDetails and inserted three documents as shown below into it-

> db.empDetails.insertMany(
	[
		{
			First_Name: "Rishika",
			Last_Name: "choudhary",
			Age: "26",
			e_mail: "rishika_choudhary.123@gmail.com",
			phone: "9000012345"
		},
		{
			First_Name: "pari",
			Last_Name: "maurya",
			Age: "27",
			e_mail: "pari_maurya.123@gmail.com",
			phone: "9000054321"
		},
		{
			First_Name: "Fathima",
			Last_Name: "Sheik",
			Age: "25",
			e_mail: "Fathima_Sheik.123@gmail.com",
			phone: "9000054321"
		}
	]
)

Update the age and email values for the document with the name 'Rishika' in the following example.

> db.empDetails.findOneAndUpdate(
	{First_Name: 'Rishika'},
	{ $set: { Age: '30',e_mail: 'rishika_newemail@gmail.com'}}
)
{
	"_id" : ObjectId("5dd6636870fb13eec3963bf5"),
	"First_Name" : "Rishika",
	"Last_Name" : "choudhary",
	"Age" : "30",
	"e_mail" : "rishika_newemail@gmail.com",
	"phone" : "9000012345"
}

MongoDB updateOne() method

This method updates a single document corresponding to the filtering given.

Syntax

The basic syntax of updateOne() method is as follows −

>db.COLLECTION_NAME.updateOne(<filter>, <update>)

Example

> db.empDetails.updateOne(
	{First_Name: 'Rishika'},
	{ $set: { Age: '30',e_mail: 'rishika_newemail@gmail.com'}}
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 0 }
>

MongoDB updateMany() method

The updateMany() method updates all documents corresponding to the filter defined.

Syntax

The basic syntax of updateMany() method is as follows –

>db.COLLECTION_NAME.update(<filter>, <update>)

Example

> db.empDetails.updateMany(
	{Age:{ $gt: "25" }},
	{ $set: { Age: '00'}}
)
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

If you retrieve the contents of the document using the find method as shown below, you can see the modified values-

> db.empDetails.find()
{ "_id" : ObjectId("5dd6636870fb13eec3963bf5"), "First_Name" : "Rishika", "Last_Name" : "choudhary", "Age" : "00", "e_mail" : "rishika_newemail@gmail.com", "phone" : "9000012345" }
{ "_id" : ObjectId("5dd6636870fb13eec3963bf6"), "First_Name" : "pari", "Last_Name" : "maurya", "Age" : "00", "e_mail" : "pari_maurya.123@gmail.com", "phone" : "9000054321" }
{ "_id" : ObjectId("5dd6636870fb13eec3963bf7"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Age" : "24", "e_mail" : "Fathima_Sheik.123@gmail.com", "phone" : "9000054321" }
>

No Sidebar ads