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 值。选择 SynchronousAsynchronous标签页以查看相应的代码:

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)
restaurants = database["restaurants"]
query_filter = {'name' : 'Bagels N Buns'}
update_operation = { '$set' :
{ 'name' : '2 Bagels 2 Buns' }
}
result = await restaurants.update_one(query_filter, update_operation)

以下示例使用 update_many() 方法更新cuisine 值为 "Pizza" 的所有文档。更新后,文档的 cuisine 值为 "Pasta"。选择SynchronousAsynchronous标签页以查看相应的代码:

restaurants = database["restaurants"]
query_filter = {'cuisine' : 'Pizza'}
update_operation = { '$set' :
{ 'cuisine' : 'Pasta' }
}
result = restaurants.update_many(query_filter, update_operation)
restaurants = database["restaurants"]
query_filter = {'cuisine' : 'Pizza'}
update_operation = { '$set' :
{ 'cuisine' : 'Pasta' }
}
result = await 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. See Collation for more information.

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就会插入一个新文档。选择SynchronousAsynchronous标签页以查看相应的代码:

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

执行更新操作时,可以指定驾驶员使用的排序规则。

排序规则是一设立特定于语言的字符串比较规则,例如字母大小写和重音符号规则。

要指定排序规则,请创建 Collation 类或 Python 字典的实例。有关要传递给 Collation 构造函数或作为键包含在字典中的选项列表,请参阅 MongoDB Server 手册中的排序规则

提示

导入排序规则

要创建 Collation 类的实例,必须从 pymongo.collation 导入。

以下示例执行与上一示例相同的更新操作,但默认默认规则为 fr_CA。选择SynchronousAsynchronous标签页以查看相应的代码:

from pymongo.collation import Collation
restaurants = database["restaurants"]
query_filter = {'cuisine' : 'Pizza'}
update_operation = { '$set' :
{ 'cuisine' : 'Pasta' }
}
result = restaurants.update_many(query_filter, update_operation,
collation=Collation(locale='fr_CA'))
from pymongo.collation import Collation
restaurants = database["restaurants"]
query_filter = {'cuisine' : 'Pizza'}
update_operation = { '$set' :
{ 'cuisine' : 'Pasta' }
}
result = await restaurants.update_many(query_filter, update_operation,
collation=Collation(locale='fr_CA'))

注意

操作排序规则覆盖默认值

当您在操作中指定排序规则时,它将覆盖集合的默认规则。

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 文档:

后退

从游标访问数据