Docs HomeMongoDB Shell

Update Documents更新文档

The MongoDB shell provides the following methods to update documents in a collection:MongoDB shell提供了以下方法来更新集合中的文档:

The examples on this page reference the Atlas sample dataset. 本页的示例引用了Atlas示例数据集You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. 您可以创建一个免费的Atlas集群,并使用示例数据填充该集群,以配合这些示例。To learn more, see Get Started with Atlas.要了解更多信息,请参阅Atlas入门

Update Operator Syntax更新运算符语法

To update a document, MongoDB provides update operators, such as $set, to modify field values.为了更新文档,MongoDB提供了更新运算符,如$set,用于修改字段值。

To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请将以下表单的更新文档传递给更新方法:

{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}

Some update operators, such as $set, create the field if the field does not exist. 如果字段不存在,则某些更新运算符(如$set)会创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。

Update a Single Document更新单个文档

Use the db.collection.updateOne() method to update the first document that matches a specified filter.使用db.collection.updateOne()方法更新与指定筛选器匹配的第一个文档。

Note

MongoDB preserves a natural sort order for documents. MongoDB保留了文档的自然排序顺序。This ordering is an internal implementation feature, and you should not rely on any particular structure within it. 这种排序是一种内部实现特性,不应该依赖于其中的任何特定结构。To learn more, see natural order.要了解更多信息,请参阅自然顺序

Example

To update the first document in the sample_mflix.movies collection where title equals "Twilight":要更新sample_mflix.movies集合中title等于"Twilight"的第一个文档,请执行以下操作:

use sample_mflix

db.movies.updateOne( { title: "Twilight" },
{
$set: {
plot: "A teenage girl risks everything–including her life–when she falls in love with a vampire."
},
$currentDate: { lastUpdated: true }
})

The update operation:更新操作:

  • Uses the $set operator to update the value of the plot field for the movie Twilight.使用$set运算符更新电影Twilightplot字段的值。
  • Uses the $currentDate operator to update the value of the lastUpdated field to the current date. 使用$currentDate运算符将lastUpdated字段的值更新为当前日期。If lastUpdated field does not exist, $currentDate will create the field. 如果lastUpdated字段不存在,则$currentDate将创建该字段。See $currentDate for details.有关详细信息,请参阅$currentDate

Update Multiple Documents更新多个文档

Use the db.collection.updateMany() to update all documents that match a specified filter.使用db.collection.updateMany()来更新与指定筛选器匹配的所有文档。

Example

To update all documents in the sample_airbnb.listingsAndReviews collection to update where security_deposit is less than 100:要更新sample_airbnb.listingsAndReviews集合中的所有文档以更新security_deposit小于100的文档,请执行以下操作:

use sample_airbnb

db.listingsAndReviews.updateMany(
{ security_deposit: { $lt: 100 } },
{
$set: { security_deposit: 100, minimum_nights: 1 }
}
)

The update operation uses the $set operator to update the value of the security_deposit field to 100 and the value of the minimum_nights field to 1.更新操作使用$set运算符将security_deposit字段的值更新为100,将minimum_nights字段的值升级为1

Replace a Document替换文档

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 contain only field/value pairs. 替换文档时,替换文档必须仅包含字段/值对。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字段,那么它的值必须与当前值相同。

Example

To replace the first document from the sample_analytics.accounts collection where account_id: 371138:要替换sample_analytics.accounts集合中account_id: 371138的第一个文档,请执行以下操作:

db.accounts.replaceOne(
{ account_id: 371138 },
{ account_id: 893421, limit: 5000, products: [ "Investment", "Brokerage" ] }
)

Run the following command to read your updated document:运行以下命令以读取更新后的文档:

db.accounts.findOne( { account_id: 893421 } )

Update Behavior更新行为

To learn more about the specific behavior of updating documents, see Behavior.若要了解有关更新文档的特定行为的详细信息,请参阅行为

Learn More了解更多信息