Docs 菜单
Docs 主页
/ / /
Pymongo 驱动程序
/

Delete Documents

在此页面上

  • Overview
  • 样本数据
  • 删除操作
  • 删除一个文档
  • 删除多个文档
  • 自定义删除操作
  • 返回值
  • API 文档

在本指南中,您可以了解如何使用 PyMongo 通过执行删除操作从 MongoDB 集合中删除文档。

删除操作可从 MongoDB 集合中删除一个或多个文档。 您可以使用 delete_one()delete_many()方法执行删除操作。

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

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

  • delete_one(),这会删除与 Atlas Search条件匹配的 第一个文档

  • delete_many(),这会删除与 Atlas Search条件匹配的 所有文档

每种删除方法都需要一个查询筛选器文档,其中指定了Atlas Search标准,以确定选择要删除的文档。 有关查询筛选器的更多信息,请参阅 MongoDB Server 手册中的查询筛选器文档部分

以下示例使用 delete_one() 方法删除restaurants集合中 name 值为 "Ready Penny Inn" 的文档。选择 SynchronousAsynchronous标签页以查看相应的代码:

query_filter = { "name": "Ready Penny Inn" }
result = restaurants.delete_one(query_filter)
query_filter = { "name": "Ready Penny Inn" }
result = await restaurants.delete_one(query_filter)

以下示例使用 delete_many() 方法删除restaurants集合中 borough 值为 "Brooklyn" 的所有文档。选择SynchronousAsynchronous标签页以查看相应的代码:

query_filter = { "borough": "Brooklyn" }
result = restaurants.delete_many(query_filter)
query_filter = { "borough": "Brooklyn" }
result = await restaurants.delete_many(query_filter)

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

属性
说明

collation

Specifies the kind of language collation to use when sorting results. See Collation for more information.

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 delete command fields guide in the MongoDB Server manual for more information.

以下代码使用 delete_many() 方法删除restaurants集合中 name 值包含字符串 "Mongo" 的所有文档。它还使用 comment 选项为操作添加注释。选择SynchronousAsynchronous标签页以查看相应的代码:

query_filter = { 'name': {'$regex': 'Mongo' }}
result = restaurants.delete_many(query_filter, comment="Deleting Mongo restaurants")
query_filter = { 'name': {'$regex': 'Mongo' }}
result = await restaurants.delete_many(query_filter, comment="Deleting Mongo restaurants")

提示

如果前面的示例使用的是delete_one()方法而不是delete_many() ,则驱动程序将仅删除name值包含"Mongo"的第一个文档。

执行删除操作时,可以指定排序规则供驾驶员使用。

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

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

提示

导入排序规则

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

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

from pymongo.collation import Collation
query_filter = { "borough": "Brooklyn" }
result = restaurants.delete_many(query_filter, collation=Collation(locale='fr_CA'))
from pymongo.collation import Collation
query_filter = { "borough": "Brooklyn" }
result = await restaurants.delete_many(query_filter, collation=Collation(locale='fr_CA'))

注意

操作排序规则覆盖默认值

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

delete_one()delete_many()方法返回DeleteResult类型。 该类型包含以下属性:

  • deleted_count,表示已删除的文档数

  • acknowledged,表示服务器是否确认结果

  • raw_result,即服务器返回的原始结果

注意

如果acknowledged 属性为False ,则 的所有其他属性在访问时都会引发DeleteResult InvalidOperation异常。如果服务器未确认写入操作,则驱动程序无法确定这些值。

如果查询筛选器与任何文档都不匹配,则驱动程序不会删除任何文档,并且deleted_count为0 。

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

后退

替换文档