English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Update Document in MongoDB

MongoDB update()and save()This method is used to update documents into the collection. The update() method updates the values in the existing documents, while the save() method replaces the existing document with the document passed in the save() method.

MongoDB Update() Method

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

Syntax

update()The basic syntax is as follows-

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Example

Assuming the mycol collection has the following data.

{ "_id": ObjectId(5983548781331adf455)), "title": "MongoDB Introduction"
{ "_id": ObjectId(5983548781331adf456)), "title": "NoSQL Introduction"
{ "_id": ObjectId(5983548781331adf457)), "title": "New Tutorial Introduction"

The following example sets the title of the document with the title "MongoDB Overview" to the new title "New MongoDB Tutorial".

>db.mycol.update({'title': 'MongoDB Overview'}, {$set: {'title': 'New MongoDB Tutorial'}})
WriteResult({ "nMatched": 1, "nUpserted": 0, "nModified": 1 )
>db.mycol.find()
{ "_id": ObjectId(5983548781331adf455)), "title": "New MongoDB Tutorial"
{ "_id": ObjectId(5983548781331adf456)), "title": "NoSQL Introduction"
{ "_id": ObjectId(5983548781331adf457)), "title": "New Tutorial Introduction"
>

By default, MongoDB updates only one document. To update multiple documents, you need to set the parameter "multi" to true.

>db.mycol.update({'title':'MongoDB Introduction'},
   {$set:{'title':'new MongoDB tutorial'}},{multi:true})

MongoDB Save() method

save()This method replaces the existing document with the new document passed in the save() method.

Syntax

MongoDB save()The basic syntax is as follows-

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

Example

The following example will use _id '5983548781331adf455'Replace document.',

>db.mycol.save(
   {
      "_id": ObjectId("507f191e810c19729de860ea"), 
		"title":"new tutorial topic",
      "by":"Basic Tutorial"
   }
)
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id": ObjectId("507f191e810c19729de860ea")
)
>db.mycol.find()
{"_id": ObjectId("507f191e810c19729de860e6), "title":"new tutorial introduction",
   "by":"Tutorials Point"}
{"_id": ObjectId("507f191e810c19729de860e6), "title":"NoSQL Introduction"}
{"_id": ObjectId("507f191e810c19729de860e6), "title":"new tutorial topic"}
>

MongoDB findOneAndUpdate() method

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

Syntax

findOneAndUpdate()The basic syntax is as follows-

>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example

Assuming we have created a collection named empDetails and inserted three documents as follows-

> db.empDetails.insertMany(
	[
		{
			First_Name: "Radhika",
			Last_Name: "Sharma",
			Age: "26",
			e_mail: "[email protected]",
			phone: ""9000012345"
		},
		{
			First_Name: "Rachel",
			Last_Name: "Christopher",
			Age: "27",
			e_mail: "[email protected]",
			phone: ""9000054321"
		},
		{
			First_Name: "Fathima",
			Last_Name: "Sheik",
			Age: "24",
			e_mail: "[email protected]",
			phone: ""9000054321"
		}
	]
)

The following example updates the age and e-mail values of the document named "Radhika".

> db.empDetails.findOneAndUpdate(
	{First_Name: 'Radhika'},
	{ $set: { $set: { Age: '}30', e_mail: '[email protected]'}}
)
{
	"_id": ObjectId("5dd6636870fb13eec3963bf5"),
	"First_Name": "Radhika",
	"Last_Name": "Sharma",
	"Age": ""30",
	"e_mail": "[email protected]",
	"phone": ""9000012345"
}

MongoDB updateOne() method

This method updates a single document that matches the given filter.

Syntax

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

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

Example

> db.empDetails.updateOne(
	{First_Name: 'Radhika'},
	{ $set: { $set: { Age: '}30', e_mail: '[email protected]'}}
)
{ "acknowledged": true, "matchedCount": 1, "modifiedCount": 0 }
>

MongoDB updateMany() method

The updateMany() method updates all documents that match the given filter.

Syntax

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

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

Example

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

If you use the find method as shown below to retrieve the content of the document, you can see the updated values-

> db.empDetails.find()
{"_id": ObjectId("5dd6636870fb13eec3963bf5"), "First_Name": "Radhika", "Last_Name": "Sharma", "Age": "00", "e_mail": "[email protected]", "phone": ""9000012345" }
{"_id": ObjectId("5dd6636870fb13eec3963bf6"), "First_Name": "Rachel", "Last_Name": "Christopher", "Age": "00", "e_mail": "Rachel_Christopher."}[email protected]", "phone": ""9000054321" }
{"_id": ObjectId("5dd6636870fb13eec3963bf7), "First_Name": "Fathima", "Last_Name": "Sheik", "Age": ""24", "e_mail": "Fathima_Sheik"[email protected]", "phone": ""9000054321" }
>