批量写操作
概述
在本指南中,您可以通过使用批量写入操作来学习如何在单个数据库调用中执行多个写入操作。
考虑一个场景,您想在一个集合中插入一个文档,更新多个其他文档,然后删除一个文档。如果您使用单独的函数,每个操作都需要自己的数据库调用。相反,您可以使用批量操作来减少对数据库的调用次数。
示例数据
本指南中的示例使用的是restaurants
集合,位于 sample_restaurants
数据库中,该数据库来自Atlas 示例数据集。要了解如何创建免费的 MongoDB Atlas 集群并加载示例数据集,请参阅Atlas 入门指南。
启动批量写入操作
在运行批量写入操作之前,调用 mongoc_collection_create_bulk_operation_with_opts()
函数。此函数返回一个类型为 mongoc_bulk_operation_t
的值,您可以使用它来存储有关要执行的批量写入的指令。
mongoc_collection_create_bulk_operation_with_opts()
函数接受以下参数
集合:指定要修改的集合
选项文档:指定用于自定义操作的选项,或
NULL
以下示例调用 mongoc_collection_create_bulk_operation_with_opts()
函数,并将 restaurants
集合作为参数传递
mongoc_bulk_operation_t *bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
然后,您可以向批量操作添加写入指令。有关更多信息,请参阅以下 定义写入操作 部分。
定义写入操作
您可以通过调用以下方法定义写入操作并将其添加到批量写入中
mongoc_bulk_operation_insert_with_opts()
mongoc_bulk_operation_update_one_with_opts()
mongoc_bulk_operation_update_many_with_opts()
mongoc_bulk_operation_replace_one_with_opts()
mongoc_bulk_operation_remove_one_with_opts()
mongoc_bulk_operation_remove_many_with_opts()
以下部分展示了如何使用这些方法来指定相应的写入操作。
插入操作
要执行插入操作,请将插入指令添加到 mongoc_bulk_operation_t
中,该操作将作为批量写入的一部分排队。
以下示例调用 mongoc_bulk_operation_insert_with_opts()
函数,传递要插入的文档和 mongoc_bulk_operation_t
值作为参数。
bson_t *insert_doc = BCON_NEW ( "name", BCON_UTF8 ("Mongo's Deli"), "cuisine", BCON_UTF8 ("Sandwiches"), "borough", BCON_UTF8 ("Manhattan"), "restaurant_id", BCON_UTF8 ("1234") ); bson_error_t error; if (!mongoc_bulk_operation_insert_with_opts (bulk, insert_doc, NULL, &error)) { fprintf (stderr, "Failed to add insert operation: %s\n", error.message); } bson_destroy (insert_doc);
要插入多个文档,请对每个文档调用 mongoc_bulk_operation_insert_with_opts()
。
更新操作
要执行更新操作,请将更新指令添加到 mongoc_bulk_operation_t
中,该操作将作为批量写入的一部分排队。
以下示例调用 mongoc_bulk_operation_update_one_with_opts()
函数,传递查询过滤器、文档更新和 mongoc_bulk_operation_t
值作为参数。
bson_t *filter_doc = BCON_NEW ("name", BCON_UTF8 ("Mongo's Deli")); bson_t *update_doc = BCON_NEW ("$set", "{", "cuisine", BCON_UTF8 ("Sandwiches and Salads"), "}"); bson_error_t error; if (!mongoc_bulk_operation_update_one_with_opts (bulk, filter_doc, update_doc, NULL, &error)) { fprintf (stderr, "Failed to add update operation: %s\n", error.message); } bson_destroy (filter_doc); bson_destroy (update_doc);
要更新多个文档,请调用 mongoc_bulk_operation_update_many_with_opts()
并传入相同的参数。这将指示驱动程序更新所有匹配查询过滤器的文档。
以下示例将更新多个操作排队到批量写入中。
bson_t *filter_doc = BCON_NEW ("name", BCON_UTF8 ("Mongo's Deli")); bson_t *update_doc = BCON_NEW ("$set", "{", "cuisine", BCON_UTF8 ("Sandwiches and Salads"), "}"); bson_error_t error; if (!mongoc_bulk_operation_update_many_with_opts (bulk, filter_doc, update_doc, NULL, &error)) { fprintf (stderr, "Failed to add update operation: %s\n", error.message); } bson_destroy (filter_doc); bson_destroy (update_doc);
替换操作
替换操作会移除指定文档的所有字段和值,并用新的值替换它们。要执行替换操作,请将替换指令添加到 mongoc_bulk_operation_t
,这会将操作排队作为批量写入的一部分。
以下示例调用 mongoc_bulk_operation_replace_one_with_opts()
函数,传递查询过滤器、替换文档和 mongoc_bulk_operation_t
值作为参数。
bson_t *filter_doc = BCON_NEW ("restaurant_id", BCON_UTF8 ("1234")); bson_t *replace_doc = BCON_NEW ( "name", BCON_UTF8 ("Mongo's Deli"), "cuisine", BCON_UTF8 ("Sandwiches and Salads"), "borough", BCON_UTF8 ("Brooklyn"), "restaurant_id", BCON_UTF8 ("5678") ); bson_error_t error; if (!mongoc_bulk_operation_replace_one_with_opts (bulk, filter_doc, replace_doc, NULL, &error)) { fprintf (stderr, "Failed to add replace operation: %s\n", error.message); } bson_destroy (filter_doc); bson_destroy (replace_doc);
要替换多个文档,对每个文档调用 mongoc_bulk_operation_replace_one_with_opts()
。
删除操作
要执行删除操作,请将删除指令添加到 mongoc_bulk_operation_t
,这会将操作排队作为批量写入的一部分。
以下示例调用 mongoc_bulk_operation_remove_one_with_opts()
函数,传递查询过滤器并传递 mongoc_bulk_operation_t
值作为参数。
bson_t *filter_doc = BCON_NEW ("restaurant_id", BCON_UTF8 ("5678")); bson_error_t error; if (!mongoc_bulk_operation_remove_one_with_opts (bulk, filter_doc, NULL, &error)) { fprintf (stderr, "Failed to add delete operation: %s\n", error.message); } bson_destroy (filter_doc);
要删除多个文档,调用 mongoc_bulk_operation_remove_many_with_opts()
函数,并传入相同的参数。这将指示驱动程序删除所有与您的查询过滤器匹配的文档。
以下示例将删除多个操作排队到批量写入中。
bson_t *filter_doc = BCON_NEW ("borough", BCON_UTF8 ("Manhattan")); bson_error_t error; if (!mongoc_bulk_operation_remove_many_with_opts (bulk, filter_doc, NULL, &error)) { fprintf (stderr, "Failed to add delete operation: %s\n", error.message); } bson_destroy (filter_doc);
运行批量操作
要运行批量写入中排队的每个写操作,调用 mongoc_bulk_operation_execute()
函数。此函数接受以下参数
mongoc_bulk_operation_t 值:包含每个写操作的指令
结果位置:指定一个指针,该指针指向可覆盖的存储,其中将包含操作结果,或
NULL
错误位置:指定一个用于错误值的存储位置,或
NULL
以下示例通过调用 mongoc_bulk_operation_execute()
函数执行本指南前面部分中指定的插入、更新、替换和删除操作。
bson_error_t error; bool result = mongoc_bulk_operation_execute (bulk, NULL, &error); if (!result) { printf ("Bulk operation error: %s\n", error.message); } mongoc_bulk_operation_destroy (bulk);
如果任何写操作失败,C 驱动程序会设置输出错误,并且不再执行任何其他操作。
自定义批量写操作
您可以通过传递一个指定选项值的BSON文档来修改mongoc_collection_create_bulk_operation_with_opts()
函数的行为。下表描述了您可以在文档中设置的选项
选项 | 描述 |
---|---|
ordered | 如果为 true ,则驱动程序按提供的顺序执行写操作。如果发生错误,则不尝试执行剩余操作。如果为 false ,则驱动程序以任意顺序执行操作并尝试执行所有操作。默认为 true 。 |
writeConcern | 指定批量操作的写关注。有关更多信息,请参阅MongoDB服务器手册中的写关注。 |
sessionId | 在指定的会话中运行批量操作。有关更多信息,请参阅MongoDB服务器手册中的服务器会话。 |
comment | 将注释附加到操作上。有关更多信息,请参阅MongoDB服务器手册中的删除命令字段指南。 |
let | 指定一个包含用于提高操作可读性的值列表的文档。值必须是常数或封闭表达式,不引用文档字段。有关更多信息,请参阅MongoDB服务器手册中的let语句。 |
以下示例调用mongoc_collection_create_bulk_operation_with_opts()
函数并将ordered
选项设置为false
bson_t opts; BSON_APPEND_BOOL (&opts, "ordered", false); bulk = mongoc_collection_create_bulk_operation_with_opts (collection, &opts); // Perform bulk operation bson_destroy (&opts); mongoc_bulk_operation_destroy (bulk);
如果无序批量写操作中的任何写操作失败,C 驱动程序仅在尝试所有操作后报告错误。
注意
无序批量操作不保证执行顺序。顺序可能不同于您列出它们的顺序,以优化运行时间。
附加信息
要了解如何执行单个写入操作,请参阅以下指南
API 文档
要了解更多关于本指南中讨论的任何函数或类型的信息,请参阅以下 API 文档