1MONGOC_BULK(3) libmongoc MONGOC_BULK(3)
2
3
4
5This tutorial explains how to take advantage of MongoDB C driver bulk write
6operation features. Executing write operations in batches reduces the number
7of network round trips, increasing write throughput.
8
10 First we need to fetch a bulk operation handle from the
11 mongoc_collection_t.
12
13 mongoc_bulk_operation_t *bulk =
14 mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
15
16 We can now start inserting documents to the bulk operation. These will
17 be buffered until we execute the operation.
18
19 The bulk operation will coalesce insertions as a single batch for each
20 consecutive call to mongoc_bulk_operation_insert(). This creates a
21 pipelined effect when possible.
22
23 To execute the bulk operation and receive the result we call
24 mongoc_bulk_operation_execute().
25
26 bulk1.c
27
28 #include <assert.h>
29 #include <mongoc/mongoc.h>
30 #include <stdio.h>
31
32 static void
33 bulk1 (mongoc_collection_t *collection)
34 {
35 mongoc_bulk_operation_t *bulk;
36 bson_error_t error;
37 bson_t *doc;
38 bson_t reply;
39 char *str;
40 bool ret;
41 int i;
42
43 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
44
45 for (i = 0; i < 10000; i++) {
46 doc = BCON_NEW ("i", BCON_INT32 (i));
47 mongoc_bulk_operation_insert (bulk, doc);
48 bson_destroy (doc);
49 }
50
51 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
52
53 str = bson_as_canonical_extended_json (&reply, NULL);
54 printf ("%s\n", str);
55 bson_free (str);
56
57 if (!ret) {
58 fprintf (stderr, "Error: %s\n", error.message);
59 }
60
61 bson_destroy (&reply);
62 mongoc_bulk_operation_destroy (bulk);
63 }
64
65 int
66 main (void)
67 {
68 mongoc_client_t *client;
69 mongoc_collection_t *collection;
70 const char *uri_string = "mongodb://localhost/?appname=bulk1-example";
71 mongoc_uri_t *uri;
72 bson_error_t error;
73
74 mongoc_init ();
75
76 uri = mongoc_uri_new_with_error (uri_string, &error);
77 if (!uri) {
78 fprintf (stderr,
79 "failed to parse URI: %s\n"
80 "error message: %s\n",
81 uri_string,
82 error.message);
83 return EXIT_FAILURE;
84 }
85
86 client = mongoc_client_new_from_uri (uri);
87 if (!client) {
88 return EXIT_FAILURE;
89 }
90
91 mongoc_client_set_error_api (client, 2);
92 collection = mongoc_client_get_collection (client, "test", "test");
93
94 bulk1 (collection);
95
96 mongoc_uri_destroy (uri);
97 mongoc_collection_destroy (collection);
98 mongoc_client_destroy (client);
99
100 mongoc_cleanup ();
101
102 return EXIT_SUCCESS;
103 }
104
105
106 Example reply document:
107
108 {"nInserted" : 10000,
109 "nMatched" : 0,
110 "nModified" : 0,
111 "nRemoved" : 0,
112 "nUpserted" : 0,
113 "writeErrors" : []
114 "writeConcernErrors" : [] }
115
117 MongoDB C driver also supports executing mixed bulk write operations. A
118 batch of insert, update, and remove operations can be executed together
119 using the bulk write operations API.
120
122 Ordered bulk write operations are batched and sent to the server in the
123 order provided for serial execution. The reply document describes the
124 type and count of operations performed.
125
126 bulk2.c
127
128 #include <assert.h>
129 #include <mongoc/mongoc.h>
130 #include <stdio.h>
131
132 static void
133 bulk2 (mongoc_collection_t *collection)
134 {
135 mongoc_bulk_operation_t *bulk;
136 bson_error_t error;
137 bson_t *query;
138 bson_t *doc;
139 bson_t *opts;
140 bson_t reply;
141 char *str;
142 bool ret;
143 int i;
144
145 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
146
147 /* Remove everything */
148 query = bson_new ();
149 mongoc_bulk_operation_remove (bulk, query);
150 bson_destroy (query);
151
152 /* Add a few documents */
153 for (i = 1; i < 4; i++) {
154 doc = BCON_NEW ("_id", BCON_INT32 (i));
155 mongoc_bulk_operation_insert (bulk, doc);
156 bson_destroy (doc);
157 }
158
159 /* {_id: 1} => {$set: {foo: "bar"}} */
160 query = BCON_NEW ("_id", BCON_INT32 (1));
161 doc = BCON_NEW ("$set", "{", "foo", BCON_UTF8 ("bar"), "}");
162 mongoc_bulk_operation_update_many_with_opts (bulk, query, doc, NULL, &error);
163 bson_destroy (query);
164 bson_destroy (doc);
165
166 /* {_id: 4} => {'$inc': {'j': 1}} (upsert) */
167 opts = BCON_NEW ("upsert", BCON_BOOL (true));
168 query = BCON_NEW ("_id", BCON_INT32 (4));
169 doc = BCON_NEW ("$inc", "{", "j", BCON_INT32 (1), "}");
170 mongoc_bulk_operation_update_many_with_opts (bulk, query, doc, opts, &error);
171 bson_destroy (query);
172 bson_destroy (doc);
173 bson_destroy (opts);
174
175 /* replace {j:1} with {j:2} */
176 query = BCON_NEW ("j", BCON_INT32 (1));
177 doc = BCON_NEW ("j", BCON_INT32 (2));
178 mongoc_bulk_operation_replace_one_with_opts (bulk, query, doc, NULL, &error);
179 bson_destroy (query);
180 bson_destroy (doc);
181
182 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
183
184 str = bson_as_canonical_extended_json (&reply, NULL);
185 printf ("%s\n", str);
186 bson_free (str);
187
188 if (!ret) {
189 printf ("Error: %s\n", error.message);
190 }
191
192 bson_destroy (&reply);
193 mongoc_bulk_operation_destroy (bulk);
194 }
195
196 int
197 main (void)
198 {
199 mongoc_client_t *client;
200 mongoc_collection_t *collection;
201 const char *uri_string = "mongodb://localhost/?appname=bulk2-example";
202 mongoc_uri_t *uri;
203 bson_error_t error;
204
205 mongoc_init ();
206
207 uri = mongoc_uri_new_with_error (uri_string, &error);
208 if (!uri) {
209 fprintf (stderr,
210 "failed to parse URI: %s\n"
211 "error message: %s\n",
212 uri_string,
213 error.message);
214 return EXIT_FAILURE;
215 }
216
217 client = mongoc_client_new_from_uri (uri);
218 if (!client) {
219 return EXIT_FAILURE;
220 }
221
222 mongoc_client_set_error_api (client, 2);
223 collection = mongoc_client_get_collection (client, "test", "test");
224
225 bulk2 (collection);
226
227 mongoc_uri_destroy (uri);
228 mongoc_collection_destroy (collection);
229 mongoc_client_destroy (client);
230
231 mongoc_cleanup ();
232
233 return EXIT_SUCCESS;
234 }
235
236
237 Example reply document:
238
239 { "nInserted" : 3,
240 "nMatched" : 2,
241 "nModified" : 2,
242 "nRemoved" : 10000,
243 "nUpserted" : 1,
244 "upserted" : [{"index" : 5, "_id" : 4}],
245 "writeErrors" : []
246 "writeConcernErrors" : [] }
247
248 The index field in the upserted array is the 0-based index of the up‐
249 sert operation; in this example, the sixth operation of the overall
250 bulk operation was an upsert, so its index is 5.
251
253 Unordered bulk write operations are batched and sent to the server in
254 arbitrary order where they may be executed in parallel. Any errors that
255 occur are reported after all operations are attempted.
256
257 In the next example the first and third operations fail due to the
258 unique constraint on _id. Since we are doing unordered execution the
259 second and fourth operations succeed.
260
261 bulk3.c
262
263 #include <assert.h>
264 #include <mongoc/mongoc.h>
265 #include <stdio.h>
266
267 static void
268 bulk3 (mongoc_collection_t *collection)
269 {
270 bson_t opts = BSON_INITIALIZER;
271 mongoc_bulk_operation_t *bulk;
272 bson_error_t error;
273 bson_t *query;
274 bson_t *doc;
275 bson_t reply;
276 char *str;
277 bool ret;
278
279 /* false indicates unordered */
280 BSON_APPEND_BOOL (&opts, "ordered", false);
281 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, &opts);
282 bson_destroy (&opts);
283
284 /* Add a document */
285 doc = BCON_NEW ("_id", BCON_INT32 (1));
286 mongoc_bulk_operation_insert (bulk, doc);
287 bson_destroy (doc);
288
289 /* remove {_id: 2} */
290 query = BCON_NEW ("_id", BCON_INT32 (2));
291 mongoc_bulk_operation_remove_one (bulk, query);
292 bson_destroy (query);
293
294 /* insert {_id: 3} */
295 doc = BCON_NEW ("_id", BCON_INT32 (3));
296 mongoc_bulk_operation_insert (bulk, doc);
297 bson_destroy (doc);
298
299 /* replace {_id:4} {'i': 1} */
300 query = BCON_NEW ("_id", BCON_INT32 (4));
301 doc = BCON_NEW ("i", BCON_INT32 (1));
302 mongoc_bulk_operation_replace_one (bulk, query, doc, false);
303 bson_destroy (query);
304 bson_destroy (doc);
305
306 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
307
308 str = bson_as_canonical_extended_json (&reply, NULL);
309 printf ("%s\n", str);
310 bson_free (str);
311
312 if (!ret) {
313 printf ("Error: %s\n", error.message);
314 }
315
316 bson_destroy (&reply);
317 mongoc_bulk_operation_destroy (bulk);
318 bson_destroy (&opts);
319 }
320
321 int
322 main (void)
323 {
324 mongoc_client_t *client;
325 mongoc_collection_t *collection;
326 const char *uri_string = "mongodb://localhost/?appname=bulk3-example";
327 mongoc_uri_t *uri;
328 bson_error_t error;
329
330 mongoc_init ();
331
332 uri = mongoc_uri_new_with_error (uri_string, &error);
333 if (!uri) {
334 fprintf (stderr,
335 "failed to parse URI: %s\n"
336 "error message: %s\n",
337 uri_string,
338 error.message);
339 return EXIT_FAILURE;
340 }
341
342 client = mongoc_client_new_from_uri (uri);
343 if (!client) {
344 return EXIT_FAILURE;
345 }
346
347 mongoc_client_set_error_api (client, 2);
348 collection = mongoc_client_get_collection (client, "test", "test");
349
350 bulk3 (collection);
351
352 mongoc_uri_destroy (uri);
353 mongoc_collection_destroy (collection);
354 mongoc_client_destroy (client);
355
356 mongoc_cleanup ();
357
358 return EXIT_SUCCESS;
359 }
360
361
362 Example reply document:
363
364 { "nInserted" : 0,
365 "nMatched" : 1,
366 "nModified" : 1,
367 "nRemoved" : 1,
368 "nUpserted" : 0,
369 "writeErrors" : [
370 { "index" : 0,
371 "code" : 11000,
372 "errmsg" : "E11000 duplicate key error index: test.test.$_id_ dup key: { : 1 }" },
373 { "index" : 2,
374 "code" : 11000,
375 "errmsg" : "E11000 duplicate key error index: test.test.$_id_ dup key: { : 3 }" } ],
376 "writeConcernErrors" : [] }
377
378 Error: E11000 duplicate key error index: test.test.$_id_ dup key: { : 1 }
379
380 The bson_error_t domain is MONGOC_ERROR_COMMAND and its code is 11000.
381
383 This feature is only available when using MongoDB 3.2 and later.
384
385 By default bulk operations are validated against the schema, if any is
386 defined. In certain cases however it may be necessary to bypass the
387 document validation.
388
389 bulk5.c
390
391 #include <assert.h>
392 #include <mongoc/mongoc.h>
393 #include <stdio.h>
394
395 static void
396 bulk5_fail (mongoc_collection_t *collection)
397 {
398 mongoc_bulk_operation_t *bulk;
399 bson_error_t error;
400 bson_t *doc;
401 bson_t reply;
402 char *str;
403 bool ret;
404
405 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
406
407 /* Two inserts */
408 doc = BCON_NEW ("_id", BCON_INT32 (31));
409 mongoc_bulk_operation_insert (bulk, doc);
410 bson_destroy (doc);
411
412 doc = BCON_NEW ("_id", BCON_INT32 (32));
413 mongoc_bulk_operation_insert (bulk, doc);
414 bson_destroy (doc);
415
416 /* The above documents do not comply to the schema validation rules
417 * we created previously, so this will result in an error */
418 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
419
420 str = bson_as_canonical_extended_json (&reply, NULL);
421 printf ("%s\n", str);
422 bson_free (str);
423
424 if (!ret) {
425 printf ("Error: %s\n", error.message);
426 }
427
428 bson_destroy (&reply);
429 mongoc_bulk_operation_destroy (bulk);
430 }
431
432 static void
433 bulk5_success (mongoc_collection_t *collection)
434 {
435 mongoc_bulk_operation_t *bulk;
436 bson_error_t error;
437 bson_t *doc;
438 bson_t reply;
439 char *str;
440 bool ret;
441
442 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
443
444 /* Allow this document to bypass document validation.
445 * NOTE: When authentication is enabled, the authenticated user must have
446 * either the "dbadmin" or "restore" roles to bypass document validation */
447 mongoc_bulk_operation_set_bypass_document_validation (bulk, true);
448
449 /* Two inserts */
450 doc = BCON_NEW ("_id", BCON_INT32 (31));
451 mongoc_bulk_operation_insert (bulk, doc);
452 bson_destroy (doc);
453
454 doc = BCON_NEW ("_id", BCON_INT32 (32));
455 mongoc_bulk_operation_insert (bulk, doc);
456 bson_destroy (doc);
457
458 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
459
460 str = bson_as_canonical_extended_json (&reply, NULL);
461 printf ("%s\n", str);
462 bson_free (str);
463
464 if (!ret) {
465 printf ("Error: %s\n", error.message);
466 }
467
468 bson_destroy (&reply);
469 mongoc_bulk_operation_destroy (bulk);
470 }
471
472 int
473 main (void)
474 {
475 bson_t *options;
476 bson_error_t error;
477 mongoc_client_t *client;
478 mongoc_collection_t *collection;
479 mongoc_database_t *database;
480 const char *uri_string = "mongodb://localhost/?appname=bulk5-example";
481 mongoc_uri_t *uri;
482
483 mongoc_init ();
484
485 uri = mongoc_uri_new_with_error (uri_string, &error);
486 if (!uri) {
487 fprintf (stderr,
488 "failed to parse URI: %s\n"
489 "error message: %s\n",
490 uri_string,
491 error.message);
492 return EXIT_FAILURE;
493 }
494
495 client = mongoc_client_new_from_uri (uri);
496 if (!client) {
497 return EXIT_FAILURE;
498 }
499
500 mongoc_client_set_error_api (client, 2);
501 database = mongoc_client_get_database (client, "testasdf");
502
503 /* Create schema validator */
504 options = BCON_NEW (
505 "validator", "{", "number", "{", "$gte", BCON_INT32 (5), "}", "}");
506 collection =
507 mongoc_database_create_collection (database, "collname", options, &error);
508
509 if (collection) {
510 bulk5_fail (collection);
511 bulk5_success (collection);
512 mongoc_collection_destroy (collection);
513 } else {
514 fprintf (stderr, "Couldn't create collection: '%s'\n", error.message);
515 }
516
517 bson_free (options);
518 mongoc_uri_destroy (uri);
519 mongoc_database_destroy (database);
520 mongoc_client_destroy (client);
521
522 mongoc_cleanup ();
523
524 return EXIT_SUCCESS;
525 }
526
527
528 Running the above example will result in:
529
530 { "nInserted" : 0,
531 "nMatched" : 0,
532 "nModified" : 0,
533 "nRemoved" : 0,
534 "nUpserted" : 0,
535 "writeErrors" : [
536 { "index" : 0,
537 "code" : 121,
538 "errmsg" : "Document failed validation" } ] }
539
540 Error: Document failed validation
541
542 { "nInserted" : 2,
543 "nMatched" : 0,
544 "nModified" : 0,
545 "nRemoved" : 0,
546 "nUpserted" : 0,
547 "writeErrors" : [] }
548
549 The bson_error_t domain is MONGOC_ERROR_COMMAND.
550
552 By default bulk operations are executed with the write_concern of the
553 collection they are executed against. A custom write concern can be
554 passed to the mongoc_collection_create_bulk_operation_with_opts()
555 method. Write concern errors (e.g. wtimeout) will be reported after all
556 operations are attempted, regardless of execution order.
557
558 bulk4.c
559
560 #include <assert.h>
561 #include <mongoc/mongoc.h>
562 #include <stdio.h>
563
564 static void
565 bulk4 (mongoc_collection_t *collection)
566 {
567 bson_t opts = BSON_INITIALIZER;
568 mongoc_write_concern_t *wc;
569 mongoc_bulk_operation_t *bulk;
570 bson_error_t error;
571 bson_t *doc;
572 bson_t reply;
573 char *str;
574 bool ret;
575
576 wc = mongoc_write_concern_new ();
577 mongoc_write_concern_set_w (wc, 4);
578 mongoc_write_concern_set_wtimeout_int64 (wc, 100); /* milliseconds */
579 mongoc_write_concern_append (wc, &opts);
580
581 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, &opts);
582
583 /* Two inserts */
584 doc = BCON_NEW ("_id", BCON_INT32 (10));
585 mongoc_bulk_operation_insert (bulk, doc);
586 bson_destroy (doc);
587
588 doc = BCON_NEW ("_id", BCON_INT32 (11));
589 mongoc_bulk_operation_insert (bulk, doc);
590 bson_destroy (doc);
591
592 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
593
594 str = bson_as_canonical_extended_json (&reply, NULL);
595 printf ("%s\n", str);
596 bson_free (str);
597
598 if (!ret) {
599 printf ("Error: %s\n", error.message);
600 }
601
602 bson_destroy (&reply);
603 mongoc_bulk_operation_destroy (bulk);
604 mongoc_write_concern_destroy (wc);
605 bson_destroy (&opts);
606 }
607
608 int
609 main (void)
610 {
611 mongoc_client_t *client;
612 mongoc_collection_t *collection;
613 const char *uri_string = "mongodb://localhost/?appname=bulk4-example";
614 mongoc_uri_t *uri;
615 bson_error_t error;
616
617 mongoc_init ();
618
619 uri = mongoc_uri_new_with_error (uri_string, &error);
620 if (!uri) {
621 fprintf (stderr,
622 "failed to parse URI: %s\n"
623 "error message: %s\n",
624 uri_string,
625 error.message);
626 return EXIT_FAILURE;
627 }
628
629 client = mongoc_client_new_from_uri (uri);
630 if (!client) {
631 return EXIT_FAILURE;
632 }
633
634 mongoc_client_set_error_api (client, 2);
635 collection = mongoc_client_get_collection (client, "test", "test");
636
637 bulk4 (collection);
638
639 mongoc_uri_destroy (uri);
640 mongoc_collection_destroy (collection);
641 mongoc_client_destroy (client);
642
643 mongoc_cleanup ();
644
645 return EXIT_SUCCESS;
646 }
647
648
649 Example reply document and error message:
650
651 { "nInserted" : 2,
652 "nMatched" : 0,
653 "nModified" : 0,
654 "nRemoved" : 0,
655 "nUpserted" : 0,
656 "writeErrors" : [],
657 "writeConcernErrors" : [
658 { "code" : 64,
659 "errmsg" : "waiting for replication timed out" }
660 ] }
661
662 Error: waiting for replication timed out
663
664 The bson_error_t domain is MONGOC_ERROR_WRITE_CONCERN if there are
665 write concern errors and no write errors. Write errors indicate failed
666 operations, so they take precedence over write concern errors, which
667 mean merely that the write concern is not satisfied yet.
668
670 This feature is only available when using MongoDB 3.4 and later.
671
672 bulk-collation.c
673
674 #include <mongoc/mongoc.h>
675 #include <stdio.h>
676
677 static void
678 bulk_collation (mongoc_collection_t *collection)
679 {
680 mongoc_bulk_operation_t *bulk;
681 bson_t *opts;
682 bson_t *doc;
683 bson_t *selector;
684 bson_t *update;
685 bson_error_t error;
686 bson_t reply;
687 char *str;
688 uint32_t ret;
689
690 /* insert {_id: "one"} and {_id: "One"} */
691 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
692 doc = BCON_NEW ("_id", BCON_UTF8 ("one"));
693 mongoc_bulk_operation_insert (bulk, doc);
694 bson_destroy (doc);
695
696 doc = BCON_NEW ("_id", BCON_UTF8 ("One"));
697 mongoc_bulk_operation_insert (bulk, doc);
698 bson_destroy (doc);
699
700 /* "One" normally sorts before "one"; make "one" come first */
701 opts = BCON_NEW ("collation",
702 "{",
703 "locale",
704 BCON_UTF8 ("en_US"),
705 "caseFirst",
706 BCON_UTF8 ("lower"),
707 "}");
708
709 /* set x=1 on the document with _id "One", which now sorts after "one" */
710 update = BCON_NEW ("$set", "{", "x", BCON_INT64 (1), "}");
711 selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}");
712 mongoc_bulk_operation_update_one_with_opts (
713 bulk, selector, update, opts, &error);
714
715 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
716
717 str = bson_as_canonical_extended_json (&reply, NULL);
718 printf ("%s\n", str);
719 bson_free (str);
720
721 if (!ret) {
722 printf ("Error: %s\n", error.message);
723 }
724
725 bson_destroy (&reply);
726 bson_destroy (update);
727 bson_destroy (selector);
728 bson_destroy (opts);
729 mongoc_bulk_operation_destroy (bulk);
730 }
731
732 int
733 main (void)
734 {
735 mongoc_client_t *client;
736 mongoc_collection_t *collection;
737 const char *uri_string = "mongodb://localhost/?appname=bulk-collation";
738 mongoc_uri_t *uri;
739 bson_error_t error;
740
741 mongoc_init ();
742
743 uri = mongoc_uri_new_with_error (uri_string, &error);
744 if (!uri) {
745 fprintf (stderr,
746 "failed to parse URI: %s\n"
747 "error message: %s\n",
748 uri_string,
749 error.message);
750 return EXIT_FAILURE;
751 }
752
753 client = mongoc_client_new_from_uri (uri);
754 if (!client) {
755 return EXIT_FAILURE;
756 }
757
758 mongoc_client_set_error_api (client, 2);
759 collection = mongoc_client_get_collection (client, "db", "collection");
760 bulk_collation (collection);
761
762 mongoc_uri_destroy (uri);
763 mongoc_collection_destroy (collection);
764 mongoc_client_destroy (client);
765
766 mongoc_cleanup ();
767
768 return EXIT_SUCCESS;
769 }
770
771
772 Running the above example will result in:
773
774 { "nInserted" : 2,
775 "nMatched" : 1,
776 "nModified" : 1,
777 "nRemoved" : 0,
778 "nUpserted" : 0,
779 "writeErrors" : [ ]
780 }
781
783 Set "w" to zero for an unacknowledged write. The driver sends unac‐
784 knowledged writes using the legacy opcodes OP_INSERT, OP_UPDATE, and
785 OP_DELETE.
786
787 bulk6.c
788
789 #include <mongoc/mongoc.h>
790 #include <stdio.h>
791
792 static void
793 bulk6 (mongoc_collection_t *collection)
794 {
795 bson_t opts = BSON_INITIALIZER;
796 mongoc_write_concern_t *wc;
797 mongoc_bulk_operation_t *bulk;
798 bson_error_t error;
799 bson_t *doc;
800 bson_t *selector;
801 bson_t reply;
802 char *str;
803 bool ret;
804
805 wc = mongoc_write_concern_new ();
806 mongoc_write_concern_set_w (wc, 0);
807 mongoc_write_concern_append (wc, &opts);
808
809 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, &opts);
810
811 doc = BCON_NEW ("_id", BCON_INT32 (10));
812 mongoc_bulk_operation_insert (bulk, doc);
813 bson_destroy (doc);
814
815 selector = BCON_NEW ("_id", BCON_INT32 (11));
816 mongoc_bulk_operation_remove_one (bulk, selector);
817 bson_destroy (selector);
818
819 ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
820
821 str = bson_as_canonical_extended_json (&reply, NULL);
822 printf ("%s\n", str);
823 bson_free (str);
824
825 if (!ret) {
826 printf ("Error: %s\n", error.message);
827 }
828
829 bson_destroy (&reply);
830 mongoc_bulk_operation_destroy (bulk);
831 mongoc_write_concern_destroy (wc);
832 bson_destroy (&opts);
833 }
834
835 int
836 main (void)
837 {
838 mongoc_client_t *client;
839 mongoc_collection_t *collection;
840 const char *uri_string = "mongodb://localhost/?appname=bulk6-example";
841 mongoc_uri_t *uri;
842 bson_error_t error;
843
844 mongoc_init ();
845
846 uri = mongoc_uri_new_with_error (uri_string, &error);
847 if (!uri) {
848 fprintf (stderr,
849 "failed to parse URI: %s\n"
850 "error message: %s\n",
851 uri_string,
852 error.message);
853 return EXIT_FAILURE;
854 }
855
856 client = mongoc_client_new_from_uri (uri);
857 if (!client) {
858 return EXIT_FAILURE;
859 }
860
861 mongoc_client_set_error_api (client, 2);
862 collection = mongoc_client_get_collection (client, "test", "test");
863
864 bulk6 (collection);
865
866 mongoc_uri_destroy (uri);
867 mongoc_collection_destroy (collection);
868 mongoc_client_destroy (client);
869
870 mongoc_cleanup ();
871
872 return EXIT_SUCCESS;
873 }
874
875
876 The reply document is empty:
877
878 { }
879
881 See the Driver Bulk API Spec, which describes bulk write operations for
882 all MongoDB drivers.
883
885 MongoDB, Inc
886
888 2017-present, MongoDB, Inc
889
890
891
892
8931.25.1 Nov 08, 2023 MONGOC_BULK(3)