Overview
在本指南中,您可以学习如何使用C驱动程序更新MongoDB集合中的文档。您可以调用 mongoc_collection_update_one() 函数来更新单个文档,也可以调用 mongoc_collection_update_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.
更新操作
您可以使用以下函数在MongoDB中执行更新操作:
mongoc_collection_update_one(),更新匹配搜索条件的第一个文档mongoc_collection_update_many(),更新与搜索条件匹配的所有文档
每个更新函数接受以下参数:
集合:指定要更新的集合。
选项文档:指定用于自定义操作的选项,或
NULL。结果位置:指定指向将包含操作结果的可覆盖存储的指针,或
NULL。错误位置:指定错误值或
NULL的位置。
更新一个文档
使用以下语法更新文档:
bson_t *query = BCON_NEW("<field to match>", BCON_UTF8("<value to match>")); bson_t *update = BCON_NEW("$set", "{", "<field name>", BCON_UTF8("<value>"), "}"); bson_error_t error; if (!mongoc_collection_update_one(collection, query, update, NULL, NULL, &error)) { fprintf(stderr, "Update one operation failed: %s\n", error.message); } bson_destroy(query); bson_destroy(update);
以下示例使用 mongoc_collection_update_one() 函数将 restaurants集合中某一文档的 name 值从 "Bagels N Buns"更新为 "2 Bagels 2 Buns":
bson_t *query = BCON_NEW("name", BCON_UTF8("Bagels N Buns")); bson_t *update = BCON_NEW("$set", "{", "name", BCON_UTF8("2 Bagels 2 Buns"), "}"); bson_error_t error; if (!mongoc_collection_update_one(collection, query, update, NULL, NULL, &error)) { fprintf(stderr, "Update one operation failed: %s\n", error.message); } bson_destroy(query); bson_destroy(update);
更新多个文档
使用以下语法更新多个文档:
bson_t *query = BCON_NEW("<field to match>", BCON_UTF8("<value to match>")); bson_t *update = BCON_NEW("$set", "{", "<field name>", BCON_UTF8("<value>"), "}"); bson_error_t error; if (!mongoc_collection_update_many(collection, query, update, NULL, NULL, &error)) { fprintf(stderr, "Update many operation failed: %s\n", error.message); } bson_destroy(query); bson_destroy(update);
以下示例使用 mongoc_collection_update_many() 函数更新cuisine 值为 "Pizza" 的所有文档。 更新后,文档的 cuisine 值为 "Pasta"。
bson_t *query = BCON_NEW("cuisine", BCON_UTF8("Pizza")); bson_t *update = BCON_NEW("$set", "{", "cuisine", BCON_UTF8("Pasta"), "}"); bson_error_t error; if (!mongoc_collection_update_many(collection, query, update, NULL, NULL, &error)) { fprintf(stderr, "Update many operation failed: %s\n", error.message); } bson_destroy(query); bson_destroy(update);
自定义更新操作
您可以通过传递指定选项值的BSON文档来修改 mongoc_collection_update_one() 和 mongoc_collection_update_many() 函数的行为。 下表描述了您可以在文档中设立的一些选项:
选项 | 说明 |
|---|---|
| 如果设置为 |
| 设置操作的写关注(write concern)。 |
| 指定比较文本时使用的语言排序规则类型。有关详细信息,请参阅 MongoDB Server 手册中的 排序规则。 |
| 附加到操作的评论。有关详细信息,请参阅 MongoDB Server 手册中的插入命令字段指南。 |
| 附加到操作的评论。有关详细信息,请参阅 MongoDB Server 手册中的插入命令字段指南。 |
更新或插入(upsert)文档
以下示例使用 mongoc_collection_update_many() 函数查找 borough 值为 "Manhattan" 的所有文档。然后,它将这些文档中的 borough 值更新为 "Manhattan (north)"。由于 upsert 选项设立为 true,因此,如果查询筛选条件与任何现有文档都不匹配, C驱动程序将插入一个新文档。
bson_t *query = BCON_NEW("borough", BCON_UTF8("Manhattan")); bson_t *update = BCON_NEW("$set", "{", "borough", BCON_UTF8("Manhattan (north)"), "}"); bson_error_t error; bson_t opts; bson_init(&opts); bson_append_bool(&opts, "upsert", -1, true); if (!mongoc_collection_update_many(collection, query, update, &opts, NULL, &error)) { fprintf(stderr, "Update many operation failed: %s\n", error.message); } bson_destroy(query); bson_destroy(update); bson_destroy(&opts);
更多信息
要了解创建查询筛选器的更多信息,请参阅指定查询指南。
API 文档
要学习;了解有关本指南中讨论的任何函数的更多信息,请参阅以下API文档: