Docs 菜单
Docs 主页
/ / /
pymongo
/

Update Documents

在此页面上

  • Overview
  • 样本数据
  • 更新操作
  • 更新一个文档
  • 更新多个文档
  • 自定义更新操作
  • 返回值
  • 更多信息
  • API 文档

在本指南中,您可以了解如何使用 PyMongo 通过使用 update_one()update_many()方法来更新 MongoDB 集合中的文档。

本指南中的示例使用 Atlas 样本数据集中sample_restaurants.restaurants集合。要了解如何创建免费的 MongoDB Atlas 集群并加载样本数据集,请参阅 PyMongo 入门教程。

您可以使用以下方法在 MongoDB 中执行更新操作:

  • update_one(),更新匹配搜索条件的第一个文档

  • update_many(),更新与搜索条件匹配的所有文档

每种更新方法都需要以下参数:

  • 查询筛选器文档,用于确定要更新的文档。 有关查询筛选器的更多信息,请参阅 MongoDB Server 手册中的查询筛选器文档部分

  • 更新文档,其中指定更新操作符(要执行的更新类型)以及应更改的字段和值。 有关更新操作符及其用法的列表,请参阅 MongoDB Server 手册中的字段更新操作符指南页面

以下示例使用update_one()方法更新restaurants集合中名为"Bagels N Buns"的文档的name值:

restaurants = database["restaurants"]
query_filter = {'name' : 'Bagels N Buns'}
update_operation = { '$set' :
{ 'name' : '2 Bagels 2 Buns' }
}
result = restaurants.update_one(query_filter, update_operation)

以下示例使用update_many()方法更新cuisine值为"Pizza"的所有文档。 更新后,文档的cuisine值为"Pasta"

restaurants = database["restaurants"]
query_filter = {'cuisine' : 'Pizza'}
update_operation = { '$set' :
{ 'cuisine' : 'Pasta' }
}
result = restaurants.update_many(query_filter, update_operation)

update_one()update_many()方法可以选择接受其他参数,这些参数表示可用于配置更新操作的选项。 如果不指定任何其他选项,驱动程序将不会自定义更新操作。

属性
说明
upsert
Specifies whether the update operation performs an upsert operation if no documents match the query filter. For more information, see the upsert statement in the MongoDB Server manual.
Defaults to False
bypass_document_validation
Specifies whether the update operation bypasses document validation. This lets you update documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual.
Defaults to False.
collation
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
array_filters
A list of filters that specifies which array elements an update applies to.
hint
Gets or sets the index to scan for documents. For more information, see the hint statement in the MongoDB Server manual.
session
An instance of ClientSession.
let
A Map of parameter names and values. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.
comment
A comment to attach to the operation. For more information, see the insert command fields guide in the MongoDB Server manual for more information.

以下代码使用update_many()方法查找borough字段值为"Manhattan"的所有文档。 然后,它将这些文档中的borough值更新为"Manhattan (north)" 。 由于upsert选项设置为True ,因此如果查询筛选器与任何现有文档都不匹配,PyMongo 就会插入一个新文档。

restaurants = database["restaurants"]
query_filter = {'borough' : 'Manhattan'}
update_operation = { '$set' :
{ 'borough' : 'Manhattan (north)' }
}
result = restaurants.update_many(query_filter, update_operation, upsert = True)

update_one()update_many() 方法会各自返回一个 UpdateResult 对象。UpdateResult 类型包含以下属性:

属性
说明
matched_count
The number of documents that matched the query filter, regardless of how many were updated.
modified_count
The number of documents modified by the update operation. If an updated document is identical to the original, it is not included in this count.
raw_result
The raw result document returned by the server.
upserted_id
The ID of the document that was upserted in the database, if the driver performed an upsert. Otherwise None.

要了解创建查询筛选器的更多信息,请参阅指定查询指南。

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档:

后退

插入文档