Mongodb 简明教程

MongoDB - Update Document

MongoDB 的 update()save() 方法用于更新集合中的文档。update() 方法更新现有文档中的值,而 save() 方法用 save() 方法传递的文档替换现有文档。

MongoDB’s update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.

MongoDB Update() Method

update() 方法更新现有文档中的值。

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

Syntax

update() 方法的基本语法如下:

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

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Example

考虑 mycol 集合具有以下数据。

Consider the mycol collection has the following data.

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

以下示例将设置标题为“MongoDB 概述”的文档的新标题“新 MongoDB 教程”。

Following example will set the new title 'New MongoDB Tutorial' of the documents whose title is 'MongoDB Overview'.

>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":"Tutorials Point Overview"}
>

默认情况下,MongoDB 将仅更新单个文档。要更新多个文档,你需要将参数“multi”设置为 true。

By default, MongoDB will update only a single document. To update multiple documents, you need to set a parameter 'multi' to true.

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

MongoDB Save() Method

save() 方法用 save() 方法中传递的新文档替换现有文档。

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

Syntax

MongoDB save() 方法的基本语法如下所示:

The basic syntax of MongoDB save() method is shown below −

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

Example

以下示例将用 _id“5983548781331adf45ec5”替换文档。

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

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

MongoDB findOneAndUpdate() method

findOneAndUpdate() 方法更新现有文档中的值。

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

Syntax

findOneAndUpdate() 方法的基本语法如下:

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

>db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example

假设我们创建了一个名为 empDetails 的集合,并在其中插入了三个文档,如下所示:

Assume we have created a collection named empDetails and inserted three documents in it as shown below −

> db.empDetails.insertMany(
	[
		{
			First_Name: "Radhika",
			Last_Name: "Sharma",
			Age: "26",
			e_mail: "radhika_sharma.123@gmail.com",
			phone: "9000012345"
		},
		{
			First_Name: "Rachel",
			Last_Name: "Christopher",
			Age: "27",
			e_mail: "Rachel_Christopher.123@gmail.com",
			phone: "9000054321"
		},
		{
			First_Name: "Fathima",
			Last_Name: "Sheik",
			Age: "24",
			e_mail: "Fathima_Sheik.123@gmail.com",
			phone: "9000054321"
		}
	]
)

以下示例更新带有名称“Radhika”的文档的年龄和电子邮件值。

Following example updates the age and email values of the document with name 'Radhika'.

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

MongoDB updateOne() method

此方法更新与给定筛选器匹配的单个文档。

This methods updates a single document which matches the given filter.

Syntax

updateOne() 方法的基本语法如下:

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

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

Example

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

MongoDB updateMany() method

updateMany() 方法更新所有与给定筛选条件匹配的文档。

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

Syntax

updateMany() 方法的基本语法如下:

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 }

您可以使用 find 方法检索文档内容以查看更新后的值,如下所示:

You can see the updated values if you retrieve the contents of the document using the find method as shown below −

> db.empDetails.find()
{ "_id" : ObjectId("5dd6636870fb13eec3963bf5"), "First_Name" : "Radhika", "Last_Name" : "Sharma", "Age" : "00", "e_mail" : "radhika_newemail@gmail.com", "phone" : "9000012345" }
{ "_id" : ObjectId("5dd6636870fb13eec3963bf6"), "First_Name" : "Rachel", "Last_Name" : "Christopher", "Age" : "00", "e_mail" : "Rachel_Christopher.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" }
>