对于 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() 函数执行删除操作。

本指南中的示例使用来自 Atlas 示例数据集sample_restaurants 数据库中的 restaurants 集合。要学习如何创建免费的MongoDB Atlas 集群并加载示例数据集,请参阅MongoDB 入门指南

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

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

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

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

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

  • Query filter document: Specifies which collection documents to delete. For more information about query filters, see the Query Filter Documents section in the MongoDB Server manual.

  • 结果位置:指定指向将包含操作结果的可覆盖存储的指针,或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

Specifies the kind of language collation to use when comparing text. For more information, see Collation in the MongoDB Server manual.
Type: bson_t

writeConcern

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

let

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

comment

A comment to attach to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.
Type: 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文档: