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. You can create a free Atlas cluster and populate that cluster with sample data to follow along with these examples. To learn more, see Get Started with Atlas.本页上的示例引用了Atlas示例数据集。您可以创建一个免费的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. See the individual update operator reference for details.$set)会创建该字段。有关详细信息,请参阅个人更新运算符参考。
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. This ordering is an internal implementation feature, and you should not rely on any particular structure within it. To learn more, see natural order.MongoDB保留了文档的自然排序顺序。这种排序是一种内部实现特性,您不应该依赖其中的任何特定结构。要了解更多信息,请参阅自然顺序。
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使用$setoperator to update the value of theplotfield for the movieTwilight.$set运算符更新电影"Twilight"《暮光之城》的plot(情节)字段值。Uses the使用$currentDateoperator to update the value of thelastUpdatedfield to the current date.$currentDate运算符将lastUpdated字段的值更新为当前日期。If如果lastUpdatedfield does not exist,$currentDatewill create the field. See$currentDatefor details.lastUpdated字段不存在,则$currentDate将创建该字段。有关详细信息,请参阅$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了解更多
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.要查看更新文档的所有可用方法,请参阅更新方法。