Update Methods更新方法

MongoDB provides the following methods for updating documents in a collection:MongoDB提供以下方法来更新集合中的文档:

db.collection.updateOne()

Updates at most a single document that match a specified filter even though multiple documents may match the specified filter.即使多个文档可能与指定的筛选器匹配,也最多更新与指定筛选器匹配的单个文档。

New in version 3.2.版本3.2中的新功能。

db.collection.updateMany()

Update all documents that match a specified filter.更新与指定筛选器匹配的所有文档。

New in version 3.2.版本3.2中的新功能。

db.collection.replaceOne()

Replaces at most a single document that match a specified filter even though multiple documents may match the specified filter.即使多个文档可能与指定的筛选器匹配,也最多替换与指定筛选器匹配的单个文档。

New in version 3.2.版本3.2中的新功能。

db.collection.update()

Either updates or replaces a single document that match a specified filter or updates all documents that match a specified filter.更新或替换与指定筛选器匹配的单个文档,或更新与指定筛选器匹配的所有文档。

By default, the db.collection.update() method updates a single document. 默认情况下,db.collection.update()方法更新单个文档。To update multiple documents, use the multi option.要更新多个文档,请使用multi选项。

Additional Methods附加方法

The following methods can also update documents from a collection:以下方法还可以更新集合中的文档:

See the individual reference pages for the methods for more information and examples.有关更多信息和示例,请参阅各个参考页中的方法。