Update Documents更新文档
On this page本页内容
The MongoDB shell provides the following methods to update documents in a collection:MongoDB shell提供了以下方法来更新集合中的文档:
To update a single document, use要更新单个文档,请使用db.collection.updateOne()
.db.collection.updateOne()
。To update multiple documents, use要更新多个文档,请使用db.collection.updateMany()
.db.collection.updateMany()
。To replace a document, use若要替换文档,请使用db.collection.replaceOne()
.db.collection.replaceOne()
。
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 为了更新文档,MongoDB提供了更新运算符,如$set
, to modify field values.$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()
方法更新与指定筛选器匹配的第一个文档。
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.要了解更多信息,请参阅自然顺序。
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 theplot
field for the movieTwilight
.$set
运算符更新电影Twilight
的plot
字段的值。Uses the使用$currentDate
operator to update the value of thelastUpdated
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()
来更新与指定筛选器匹配的所有文档。
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
字段,那么它的值必须与当前值相同。
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了解更多信息
To learn how to update documents using an aggregation pipeline, see Updates with Aggregation Pipeline.要了解如何使用聚合管道更新文档,请参阅使用聚合管道进行更新。To see all available methods to update documents, see Update Methods.要查看更新文档的所有可用方法,请参阅更新方法。