对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

Delete Documents

在本指南中,您可以学习如何使用C驱动程序通过执行删除操作从MongoDB集合中删除文档。

删除操作可从MongoDB集合中删除一个或多个文档。您可以使用 mongoc_collection_delete_one()mongoc_collection_delete_many() 函数执行删除操作。

The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the MongoDB Get Started guide.

您可以使用以下函数执行删除操作:

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

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

每个删除函数接受以下参数:

  • 集合:指定要修改的集合。

  • 查询过滤文档:指定要删除的集合文档。有关查询筛选器的更多信息,请参阅MongoDB Server手册中的查询筛选器文档部分。

  • 结果位置:指定指向将包含操作结果的可覆盖存储的指针,或NULL

  • 错误位置:指定错误值或NULL 的位置。

使用以下语法删除文档:

bson_t *filter = BCON_NEW("<field name>", BCON_UTF8("<value>"));
bson_error_t error;
if (!mongoc_collection_delete_one(collection, filter, NULL, NULL, &error)) {
fprintf(stderr, "Delete error: %s\n", error.message);
}
bson_destroy(filter);

以下示例使用 mongoc_collection_delete_one() 函数删除restaurants集合中 name 值为 "Ready Penny Inn" 的文档:

bson_t *filter = BCON_NEW("name", BCON_UTF8("Ready Penny Inn"));
bson_error_t error;
if (!mongoc_collection_delete_one(collection, filter, NULL, NULL, &error)) {
printf("Delete error: %s\n", error.message);
}
bson_destroy(filter);

使用以下语法删除多个文档:

bson_t *filter = BCON_NEW("<field name>", BCON_UTF8("<value>"));
bson_error_t error;
if (!mongoc_collection_delete_many(collection, filter, NULL, NULL, &error)) {
fprintf(stderr, "Delete error: %s\n", error.message);
}
bson_destroy(filter);

以下示例使用 mongoc_collection_delete_many() 函数删除restaurants集合中 borough 值为 "Brooklyn" 的所有文档:

bson_t *filter = BCON_NEW("borough", BCON_UTF8("Brooklyn"));
bson_error_t error;
if (!mongoc_collection_delete_many(collection, filter, NULL, NULL, &error)) {
printf("Delete error: %s\n", error.message);
}
bson_destroy(filter);

您可以通过传递指定选项值的BSON文档来修改 mongoc_collection_delete_one()mongoc_collection_delete_many() 函数的行为。 下表描述了您可以在文档中设立的一些选项:

字段
说明

collation

指定比较文本时使用的语言排序规则类型。有关详细信息,请参阅 MongoDB Server 手册中的 排序规则
类型bson_t

writeConcern

设置操作的写关注(write concern)。
默认为命名空间的写关注(write concern)。
类型mongoc_write_concern_t

let

指定包含值列表的文档,以提高操作可读性。值必须是常量或不引用文档字段的封闭表达式。有关详细信息,请参阅 MongoDB Server 手册中的 let 声明
类型bson_t

comment

附加到操作的评论。有关详细信息,请参阅 MongoDB Server 手册中的插入命令字段指南。
类型bson_value_t

以下示例调用 mongoc_collection_delete_many() 函数来删除restaurants集合中 name 值包含字符串 "Mongo" 的所有文档。 它还设置 comment 选项,为操作添加注释:

bson_t *filter = BCON_NEW("name", "{", "$regex", BCON_UTF8("Mongo"), "}");
bson_error_t error;
bson_t opts;
bson_init(&opts);
BCON_APPEND(&opts, "comment", BCON_UTF8("Deleting Mongo restaurants"));
if (!mongoc_collection_delete_many(collection, filter, &opts, NULL, &error)) {
printf("Delete error: %s\n", error.message);
}
bson_destroy(filter);
bson_destroy(&opts);

提示

如果在上示例中使用 mongoc_collection_delete_one() 函数而不是 mongoc_collection_delete_many(),则驱动程序仅删除 name 值包含 "Mongo" 的第一个文档。

要学习;了解有关本指南中讨论的任何函数的更多信息,请参阅以下API文档: