This page provides examples in:本页提供了以下示例:
This page uses the following 此页面使用以下mongo
shell methods:mongo
shell方法:
db.collection.updateOne(<filter>, <update>, <options>)
db.collection.updateMany(<filter>, <update>, <options>)
db.collection.replaceOne(<filter>, <update>, <options>)
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To create and/or populate the 要创建和/或填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page uses MongoDB Compass to update the documents.此页面使用MongoDB Compass更新文档。
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。Populate the 使用以下文档填充inventory
collection with the following documents:inventory
集合:
This page uses the following PyMongo Python driver methods:
pymongo.collection.Collection.update_one()
pymongo.collection.Collection.update_many()
pymongo.collection.Collection.replace_one()
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To create and/or populate the 要创建和/或填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page uses the following Java Synchronous Driver methods:此页面使用以下Java同步驱动程序方法:
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To create and/or populate the 要创建和/或填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page uses the following MongoDB Node.js Driver methods:此页面使用以下MongoDB Node.js驱动程序方法:
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To create and/or populate the 要创建和/或填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page uses the following MongoDB PHP Library methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following Motor driver methods:
motor.motor_asyncio.AsyncIOMotorCollection.update_one()
motor.motor_asyncio.AsyncIOMotorCollection.update_many()
motor.motor_asyncio.AsyncIOMotorCollection.replace_one()
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following Java Reactive Streams Driver methods:此页面使用以下Java反应流驱动程序方法:
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To create and/or populate the 要创建和/或填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page uses the following MongoDB C# Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Perl Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Ruby Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Scala Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Go Driver functions:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
For instructions on inserting documents using MongoDB Compass, see Insert Documents.有关使用MongoDB Compass插入文档的说明,请参阅插入文档。
To update a document, MongoDB provides update operators, such as 为了更新文档,MongoDB提供了更新运算符(如$set
, to modify field values.$set
)来修改字段值。
To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请向更新方法传递以下表单的更新文档:
Some update operators, such as 如果字段不存在,某些更新运算符(如$set
, will create the field if the field does not exist. $set
)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。
To update a document in Compass, hover over the target document and click the pencil icon:要在Compass中更新文档,请将鼠标悬停在目标文档上,然后单击铅笔图标:
After clicking the pencil icon, the document enters edit mode:单击铅笔图标后,文档进入编辑模式:
You can now change the this document by clicking the item you wish to change and modifying the value.现在,您可以通过单击要更改的项目并修改值来更改此文档。
For detailed instructions on updating documents in Compass, refer to the Compass documentation or follow the example below.有关在Compass中更新文档的详细说明,请参阅Compass文档或遵循以下示例。
Once you are satisfied with your changes, click Update to save the updated document.对更改感到满意后,单击“更新”保存更新的文档。
Click Cancel to revert any modifications made to the document and exit edit mode.单击“取消”恢复对文档所做的任何修改并退出编辑模式。
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请向更新方法传递以下表单的更新文档:
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as 为了更新文档,MongoDB提供了更新运算符(如$set
to modify field values.$set
)来修改字段值。
The driver provides the com.mongodb.client.model.Updates class to facilitate the creation of update documents. 驱动程序提供com.mongodb.client.model.Updates
类,以方便创建更新文档。For example:例如:
For a list of the update helpers, see com.mongodb.client.model.Updates.有关更新帮助程序的列表,请参阅com.mongodb.client.model.Updates
。
Some update operators, such as 如果字段不存在,某些更新运算符(如$set
, will create the field if the field does not exist. $set
)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。
To update a document, MongoDB provides update operators such as 为了更新文档,MongoDB提供了更新运算符(如$set
to modify field values.$set
)来修改字段值。
To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请向更新方法传递以下表单的更新文档:
Some update operators, such as 如果字段不存在,某些更新运算符(如$set
, will create the field if the field does not exist. $set
)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请向更新方法传递以下表单的更新文档:
Some update operators, such as 如果字段不存在,某些更新运算符(如$set
, will create the field if the field does not exist. $set
)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。
To update a document, MongoDB provides update operators such as 为了更新文档,MongoDB提供了更新运算符(如$set
to modify field values.$set
)来修改字段值。
The driver provides the com.mongodb.client.model.Updates class to facilitate the creation of update documents. 驱动程序提供com.mongodb.client.model.Updates
类,以方便创建更新文档。For example:例如:
For a list of the update helpers, see com.mongodb.client.model.Updates.有关更新帮助程序的列表,请参阅com.mongodb.client.model.Updates
。
Some update operators, such as 如果字段不存在,某些更新运算符(如$set
, will create the field if the field does not exist. $set
)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
Note
Starting in MongoDB 4.2, MongoDB can accept an aggregation pipeline to specify the modifications to make instead of an update document. 从MongoDB 4.2开始,MongoDB可以接受聚合管道来指定要进行的修改,而不是更新文档。See the method reference page for details.有关详细信息,请参阅方法参考页。
The following example uses the 下面的示例使用db.collection.updateOne()
method on the inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的db.collection.updateOne()
方法更新第一个其item
等于"paper"
的文档:
The following example demonstrates using MongoDB Compass to modify a single document where 下面的示例演示如何使用MongoDB Compass修改item: paper
in the inventory
collection:inventory
集合中包含item: paper
的单个文档:
Note
This example uses the Compass Table View to modify the document. 本例使用Compass 表格视图修改文档。The editing process using the Compass List View follows a very similar approach.使用Compass列表视图的编辑过程遵循非常类似的方法。
For more information on the differences between Table View and List View in Compass, refer to the Compass documentation.有关Compass中表格视图和列表视图之间差异的更多信息,请参阅Compass文档。
The following example uses the 以下示例使用update_one()
method on the inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的update_one()
方法更新第一个其item
等于"paper"
的文档:
The following example uses the com.mongodb.client.MongoCollection.updateOne method on the 以下示例使用inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的com.mongodb.client.MongoCollection.updateOne方法更新第一个其item
等于"paper"
的文档:
The following example uses the Collection.updateOne() method on the 以下示例使用inventory
collection to update the first
document where item
equals "paper"
:inventory
集合上的Collection.updateOne()方法更新第一个其item
等于"paper"
的文档:
The following example uses the 下面的示例使用updateOne()
method on the inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的updateOne()
方法更新第一个其item
等于"paper"
的文档:
The following example uses the update_one()
method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the com.mongodb.reactivestreams.client.MongoCollection.updateOne on the 以下示例使用inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的com.mongodb.reactivestreams.client.MongoCollection.updateOne来更新item
等于"paper"
的第一个文档:
The following example uses the IMongoCollection.UpdateOne() method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the update_one() method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the update_one() method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the updateOne() method on the inventory
collection to update the first
document where item
equals "paper"
:
The following example uses the Collection.UpdateOne method on the inventory
collection to update the first document where item
equals "paper"
:
The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。Modify the target document as follows:按如下方式修改目标文档:
status
field from D
to P
.status
字段从D
更改为P
。size.uom
field from in
to cm
.size.uom
字段从in
更改为cm
。lastModified
whose value will be today’s date.lastModified
的新字段,其值将是今天的日期。Copy the following filter document into the query bar and click Find:将以下筛选文档复制到查询栏中,然后单击“查找”:
status
field and click the pencil icon which appears on the right side of the document to enter edit mode:status
字段上,然后单击文档右侧出现的铅笔图标,以进入编辑模式:
"P"
."P"
。size
field and click the outward-pointing arrows which appear on the right side of the field. size
字段上,然后单击字段右侧显示的向外箭头。size
object:size
对象中的字段:
status
field, change the value of the size.uom
field to "cm"
.status
字段的相同过程,将size.uom
字段的值更改为"cm"
。inventory
to return to the original table view, which displays the top-level document:inventory
的表格上方最左侧的选项卡,返回原始表格视图,其中显示顶级文档:
status
field and click the pencil icon which appears on the right side of the document to re-enter edit mode.status
字段上,然后单击文档右侧出现的铅笔图标以重新进入编辑模式。status
field and click the plus button icon which appears in the edit menu.status
字段内部,然后单击“编辑”菜单中显示的“加号”按钮图标。
Click the Add Field After status button which appears below the plus button:单击加号按钮下方显示的“status后添加字段”按钮:
lastModified
with a value of today’s date. lastModified
的新字段,其值为今天的日期。Date
:Date
:
Note
Because MongoDB Compass does not support 由于MongoDB Compass不支持$currentDate
or any other Field Update Operators, you must manually enter the date value in Compass.$currentDate
或任何其他字段更新运算符,因此必须在Compass中手动输入日期值。
The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。See $currentDate
for details.New in version 3.2.版本3.2中的新功能。
The following example uses the 以下示例使用db.collection.updateMany()
method on the inventory
collection to update all documents where qty
is less than 50
:inventory
集合上的db.collection.updateMany()
方法更新qty
小于50
的所有文档:
New in version 3.2.版本3.2中的新功能。
The following example uses the update_many()
method on the inventory
collection to update all documents where qty
is less than 50
:
The following example uses the com.mongodb.client.MongoCollection.updateMany method on the 以下示例使用inventory
collection to update all documents where qty
is less than 50
:inventory
集合上的com.mongodb.client.MongoCollection.updateMany
方法更新qty
小于50
的所有文档:
The following example uses the Collection.updateMany() method on the 以下示例使用inventory
collection to update all documents where qty
is less than 50
:inventory
集合上的Collection.updateMany()方法更新qty
小于50
的所有文档:
The following example uses the updateMany()
method on the inventory
collection to update all documents where qty
is less than 50
:
New in version 3.2.版本3.2中的新功能。
The following example uses the update_many()
method on the inventory
collection to update all documents where qty
is less than 50
:
The following example uses the com.mongodb.reactivestreams.client.MongoCollection.updateMany method on the 以下示例使用inventory
collection to update all documents where qty
is less than 50
:inventory
集合上的com.mongodb.reactivestreams.client.MongoCollection.updateMany
方法更新qty
小于50
的所有文档:
The following example uses the IMongoCollection.UpdateMany() method on the inventory
collection to update all documents where qty
is less than 50
:
The following example uses the update_many() method on the inventory
collection to update all documents where qty
is less than 50
:
The following example uses the update_many() method on the inventory
collection to update all documents where qty
is less than 50
:
The following example uses the updateMany() method on the inventory
collection to update all documents where qty
is less than 50
:
The following example uses the Collection.UpdateMany method on the inventory
collection to update all documents where qty
is less than 50
:
The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"in"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"in"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"in"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"in"
,将status
字段的值更新为"P"
,$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。$currentDate
for details.$currentDate
。The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.The update operation:更新操作:
$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.To replace the entire content of a document except for the 要替换文档中除_id
field, pass an entirely new document as the second argument to db.collection.replaceOne()
._id
字段之外的全部内容,请将一个全新文档作为第二个参数传递给db.collection.replaceOne()
。
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.替换文档时,替换文档必须仅包含字段/值对;亦即,不包括更新运算符表达式。
The replacement document can have different fields from the original document. 替换文档可以具有与原始文档不同的字段。In the replacement document, you can omit the 在替换文档中,您可以省略_id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value._id
字段,因为_id
字段是不可变的;但是,如果确实包含_id
字段,则该字段的值必须与当前值相同。
The following example replaces the first document from the 以下示例替换库存集合中的第一个满足inventory
collection where item: "paper"
:item: "paper"
的文档:
To replace the entire content of a document except for the 要替换文档中除_id
field, pass an entirely new document as the second argument to replace_one()
._id
字段之外的全部内容,请将一个全新文档作为第二个参数传递给replace_one()
。
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.替换文档时,替换文档必须仅包含字段/值对;亦即,不包括更新运算符表达式。
The replacement document can have different fields from the original document. 替换文档可以具有与原始文档不同的字段。In the replacement document, you can omit the 在替换文档中,您可以省略_id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value._id
字段,因为_id
字段是不可变的;但是,如果确实包含_id
字段,则该字段的值必须与当前值相同。
The following example replaces the first document from the inventory
collection where item: "paper"
:
To replace the entire content of a document except for the 要替换文档中除_id
field, pass an entirely new document as the second argument to com.mongodb.client.MongoCollection.replaceOne._id
字段之外的全部内容,请将全新文档作为第二个参数传递给com.mongodb.client.MongoCollection.replaceOne
。
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.替换文档时,替换文档必须仅包含字段/值对;亦即,不包括更新运算符表达式。
The replacement document can have different fields from the original document. 替换文档可以具有与原始文档不同的字段。In the replacement document, you can omit the 在替换文档中,您可以省略_id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value._id
字段,因为_id
字段是不可变的;但是,如果确实包含_id
字段,则该字段的值必须与当前值相同。
The following example replaces the first document from the 以下示例替换inventory
collection where item: "paper"
:inventory
集合中的第一个满足item: "paper"
的文档:
To replace the entire content of a document except for the 要替换文档中除_id
field, pass an entirely new document as the second argument to Collection.replaceOne()._id
字段之外的全部内容,请将全新文档作为第二个参数传递给Collection.replaceOne()。
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.替换文档时,替换文档必须仅包含字段/值对;亦即,不包括更新运算符表达式。
The replacement document can have different fields from the original document. 替换文档可以具有与原始文档不同的字段。In the replacement document, you can omit the 在替换文档中,您可以省略_id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value._id
字段,因为_id
字段是不可变的;但是,如果确实包含_id
字段,则该字段的值必须与当前值相同。
The following example replaces the first document from the 以下示例替换inventory
collection where item: "paper"
:inventory
集合中的第一个满足item: "paper"
的文档:
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to replaceOne()
.
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value.
The following example replaces the first document from the inventory
collection where item: "paper"
:
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to replace_one()
.
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value.
The following example replaces the first document from the inventory
collection where item: "paper"
:
To replace the entire content of a document except for the 要替换文档中除_id
field, pass an entirely new document as the second argument to com.mongodb.reactivestreams.client.MongoCollection.replaceOne._id
字段之外的全部内容,请将全新文档作为第二个参数传递给com.mongodb.reactivestreams.client.MongoCollection.replaceOne。
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.替换文档时,替换文档必须仅包含字段/值对;亦即,不包括更新运算符表达式。
The replacement document can have different fields from the original document. 替换文档可以具有与原始文档不同的字段。In the replacement document, you can omit the 在替换文档中,您可以省略_id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value._id
字段,因为_id
字段是不可变的;但是,如果确实包含_id
字段,则该字段的值必须与当前值相同。
The following example replaces the first document from the 以下示例替换inventory
collection where item: "paper"
:inventory
集合中的第一个满足item: "paper"
的文档:
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to IMongoCollection.ReplaceOne().
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value.
The following example replaces the first document from the inventory
collection where item: "paper"
:
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to replace_one().
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value.
The following example replaces the first document from the inventory
collection where item: "paper"
:
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to replace_one().
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value.
The following example replaces the first document from the inventory
collection where item: "paper"
:
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to replaceOne()
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value.
The following example replaces the first document from the inventory
collection where item: "paper"
:
To replace the entire content of a document except for the _id
field, pass an entirely new document as the second argument to Collection.ReplaceOne.
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
The replacement document can have different fields from the original document. In the replacement document, you can omit the _id
field since the _id
field is immutable; however, if you do include the _id
field, it must have the same value as the current value.
The following example replaces the first document from the inventory
collection where item: "paper"
:
All write operations in MongoDB are atomic on the level of a single document. MongoDB中的所有写操作都是单个文档级别的原子操作。For more information on MongoDB and atomicity, see Atomicity and Transactions.有关MongoDB和原子性的更多信息,请参阅原子性和事务。
_id
Once set, you cannot update the value of the 设置后,您无法更新_id
field nor can you replace an existing document with a replacement document that has a different _id
field value._id
字段的值,也无法使用具有不同_id
字段值的替换文档替换现有文档。
MongoDB preserves the order of the document fields following write operations except for the following cases:MongoDB在写操作后保留文档字段的顺序,但以下情况除外:
_id
field is always the first field in the document._id
字段始终是文档中的第一个字段。renaming
of field names may result in the reordering of fields in the document.renaming
字段名的更新可能会导致文档中字段的重新排序。If 如果updateOne()
, updateMany()
, or replaceOne()
includes upsert : true
and no documents match the specified filter, then the operation creates a new document and inserts it. updateOne()
、updateMany()
或replaceOne()
包含upsert: true
且没有与指定筛选器匹配的文档,则该操作将创建一个新文档并将其插入。If there are matching documents, then the operation modifies or replaces the matching document or documents.如果存在匹配文档,则该操作将修改或替换匹配文档。
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If update_one()
, update_many()
, or replace_one()
includes upsert : true
and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If the update and replace methods include the com.mongodb.client.model.UpdateOptions parameter that specifies com.mongodb.client.model.UpdateOptions.upsert(true) and no documents match the specified filter, then the operation creates a new document and inserts it. 如果更新和替换方法包括com.mongodb.client.model.UpdateOptions
参数,该参数指定com.mongodb.client.model.UpdateOptions.upsert(true)
,并且没有与指定筛选器匹配的文档,则操作将创建一个新文档并插入它。If there are matching documents, then the operation modifies or replaces the matching document or documents.如果存在匹配文档,则该操作将修改或替换匹配文档。
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If updateOne(), updateMany(), or replaceOne() include 如果updateOne()、updateMany()或replaceOne()在upsert : true
in the options
parameter document and
no documents match the specified filter, then the operation creates a new document and inserts it. options
参数文档中包含upsert: true
,并且没有与指定筛选器匹配的文档,则该操作将创建一个新文档并将其插入。If there are matching documents, then the operation modifies or replaces the matching document or documents.如果存在匹配文档,则该操作将修改或替换匹配文档。
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If 如果updateOne()
, updateMany()
, or replaceOne()
includes upsert => true
and no documents match the specified filter, then the operation creates a new document and inserts it. updateOne()
、updateMany()
或replaceOne()
包含upsert => true
,并且没有与指定筛选器匹配的文档,则该操作将创建一个新文档并将其插入。If there are matching documents, then the operation modifies or replaces the matching document or documents.如果存在匹配文档,则该操作将修改或替换匹配文档。
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If update_one()
, update_many()
, or replace_one()
includes upsert : true
and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If the update and replace methods include the UpdateOptions parameter that specifies UpdateOptions.upsert(true) and no documents match the specified filter, then the operation creates a new document and inserts it. 如果更新方法和替换方法包含指定UpdateOptions.upsert(true)的UpdateOptions参数,并且没有与指定筛选器匹配的文档,则该操作将创建一个新文档并将其插入。If there are matching documents, then the operation modifies or replaces the matching document or documents.如果存在匹配文档,则该操作将修改或替换匹配文档。
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If UpdateOne(), UpdateMany(), or ReplaceOne() includes an UpdateOptions argument instance with the IsUpsert
option set to true
and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If update_one(), update_many(), or replace_one() includes upsert => true
and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If update_one(), update_many(), or replace_one() includes upsert => true
and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If updateOne(), updateMany(), or replaceOne() includes upsert => true
and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
If Collection.UpdateOne includes the Upsert option set to true and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
For details on the new document created, see the individual reference pages for the methods.有关创建的新文档的详细信息,请参阅方法的各个参考页。
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. 对于写操作,您可以指定MongoDB为写操作请求的确认级别。For details, see Write Concern.有关详细信息,请参阅写关注点。
See also参阅
See also参阅
motor.motor_asyncio.AsyncIOMotorCollection.update_one()
motor.motor_asyncio.AsyncIOMotorCollection.update_many()
motor.motor_asyncio.AsyncIOMotorCollection.replace_one()