Skip to content

Commit a3bcd34

Browse files
authored
DOCSP-39625: Add new bulk write API info (#12)
* DOCSP-39625: Add new bulk write API info * edits * further reading * rewordig, deleting * add back section * JS feedback * more edits * JS feedback 2
1 parent 72727bb commit a3bcd34

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

source/libmongoc/guides/bulk.txt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ This tutorial explains how to take advantage of MongoDB C driver bulk write oper
1414
Executing write operations in batches reduces the number of network round trips, increasing write
1515
throughput.
1616

17+
.. tip::
18+
19+
Beginning in MongoDB Server version 8.0 and C Driver version 1.28, you can use the `mongoc_bulkwrite_t
20+
<{+api-libmongoc+}/mongoc_bulkwrite_t.html>`__ type. This type uses the ``bulkWrite`` server command to
21+
perform multiple kinds of write operations against mixed namespaces in a single request. For more
22+
information, see the :ref:`bulkwrite_server` section of this guide.
23+
1724
Bulk Insert
1825
-----------
1926

@@ -229,9 +236,40 @@ The ``reply`` document is empty:
229236

230237
{ }
231238

239+
.. _bulkwrite_server:
240+
241+
``bulkWrite`` Server Command
242+
----------------------------
243+
244+
The previous examples on this page use the `mongoc_bulk_operation_t <{+api-libmongoc+}/mongoc_bulk_operation_t.html>`__
245+
type to perform bulk write operations. ``mongoc_bulk_operation_t`` runs the ``insert``, ``update`` and ``delete`` server
246+
commands. Bulk write operations that use ``mongoc_bulk_operation_t`` group operations by type and send them in
247+
separate commands. You can specify only one collection for each bulk write.
248+
249+
Alternatively, you can perform bulk writes by using the `mongoc_bulkwrite_t <{+api-libmongoc+}/mongoc_bulkwrite_t.html>`__
250+
type, which runs the ``bulkWrite`` server command. The ``bulkWrite`` command was introduced in MongoDB Server 8.0 and
251+
supports insert, update, and delete operations on multiple namespaces in the same payload.
252+
253+
When connected to MongoDB Server version 8.0 or later, we recommend using ``mongoc_bulkwrite_t`` over
254+
``mongoc_bulk_operation_t`` to reduce network round trips.
255+
256+
Example
257+
~~~~~~~
258+
259+
The following example demonstrates how to use ``mongoc_bulkwrite_t`` and ``mongoc_bulkwrite_execute()`` to
260+
insert documents into two different collections:
261+
262+
.. literalinclude:: /libmongoc/includes/examples/bulk/bulkwrite.c
263+
:emphasize-lines: 24, 43
264+
:language: c
265+
:caption: bulkwrite.c
266+
232267
Further Reading
233268
---------------
234269

235-
See the `Driver Bulk API Spec <https://github.com/mongodb/specifications/blob/master/source/driver-bulk-update.rst>`__,
236-
which describes bulk write operations for all MongoDB drivers.
270+
For more information about bulk write operations, see the following bulk API specifications:
271+
272+
- Original `Driver Bulk API Spec <https://github.com/mongodb/specifications/blob/master/source/driver-bulk-update.rst>`__
273+
- New `Driver Bulk API Spec <https://github.com/mongodb/specifications/blob/master/source/crud/bulk-write.md>`__, introduced
274+
in MongoDB Server 8.0
237275

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// example-bulkwrite shows use of `mongoc_client_bulkwrite`.
2+
3+
#include <mongoc/mongoc.h>
4+
5+
#define HANDLE_ERROR(...) \
6+
if (1) { \
7+
fprintf (stderr, __VA_ARGS__); \
8+
fprintf (stderr, "\n"); \
9+
goto fail; \
10+
} else \
11+
(void) 0
12+
13+
int
14+
main (int argc, char *argv[])
15+
{
16+
bool ok = false;
17+
18+
mongoc_init ();
19+
20+
bson_error_t error;
21+
mongoc_client_t *client = mongoc_client_new ("mongodb://localhost:27017");
22+
mongoc_bulkwriteopts_t *bwo = mongoc_bulkwriteopts_new ();
23+
mongoc_bulkwriteopts_set_verboseresults (bwo, true);
24+
mongoc_bulkwrite_t *bw = mongoc_client_bulkwrite_new (client);
25+
26+
// Insert a document to `db.coll1`
27+
{
28+
bson_t *doc = BCON_NEW ("foo", "bar");
29+
if (!mongoc_bulkwrite_append_insertone (bw, "db.coll1", doc, NULL, &error)) {
30+
HANDLE_ERROR ("Error appending insert one: %s", error.message);
31+
}
32+
bson_destroy (doc);
33+
}
34+
// Insert a document to `db.coll2`
35+
{
36+
bson_t *doc = BCON_NEW ("foo", "baz");
37+
if (!mongoc_bulkwrite_append_insertone (bw, "db.coll2", doc, NULL, &error)) {
38+
HANDLE_ERROR ("Error appending insert one: %s", error.message);
39+
}
40+
bson_destroy (doc);
41+
}
42+
43+
mongoc_bulkwritereturn_t bwr = mongoc_bulkwrite_execute (bw, bwo);
44+
45+
// Print results.
46+
{
47+
BSON_ASSERT (bwr.res); // Has results. NULL only returned for unacknowledged writes.
48+
printf ("Insert count : %" PRId64 "\n", mongoc_bulkwriteresult_insertedcount (bwr.res));
49+
const bson_t *ir = mongoc_bulkwriteresult_insertresults (bwr.res);
50+
BSON_ASSERT (ir); // Has verbose results. NULL only returned if verbose results not requested.
51+
char *ir_str = bson_as_relaxed_extended_json (ir, NULL);
52+
printf ("Insert results : %s\n", ir_str);
53+
bson_free (ir_str);
54+
}
55+
56+
// Print all error information. To observe: try setting the `_id` fields to cause a duplicate key error.
57+
if (bwr.exc) {
58+
const char *msg = "(none)";
59+
if (mongoc_bulkwriteexception_error (bwr.exc, &error)) {
60+
msg = error.message;
61+
}
62+
const bson_t *we = mongoc_bulkwriteexception_writeerrors (bwr.exc);
63+
char *we_str = bson_as_relaxed_extended_json (we, NULL);
64+
const bson_t *wce = mongoc_bulkwriteexception_writeconcernerrors (bwr.exc);
65+
char *wce_str = bson_as_relaxed_extended_json (wce, NULL);
66+
const bson_t *er = mongoc_bulkwriteexception_errorreply (bwr.exc);
67+
char *er_str = bson_as_relaxed_extended_json (er, NULL);
68+
printf ("Top-level error : %s\n", msg);
69+
printf ("Write errors : %s\n", we_str);
70+
printf ("Write concern errors : %s\n", wce_str);
71+
printf ("Error reply : %s\n", er_str);
72+
bson_free (er_str);
73+
bson_free (wce_str);
74+
bson_free (we_str);
75+
}
76+
77+
mongoc_bulkwriteresult_destroy (bwr.res);
78+
mongoc_bulkwriteexception_destroy (bwr.exc);
79+
mongoc_bulkwrite_destroy (bw);
80+
81+
ok = true;
82+
fail:
83+
mongoc_client_destroy (client);
84+
mongoc_bulkwriteopts_destroy (bwo);
85+
mongoc_cleanup ();
86+
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
87+
}

0 commit comments

Comments
 (0)