1MONGOC_BULK(3)                     libmongoc                    MONGOC_BULK(3)
2
3
4

NAME

6       mongoc_bulk - Bulk Write Operations
7
8       This  tutorial  explains how to take advantage of MongoDB C driver bulk
9       write operation features. Executing write operations in batches reduces
10       the number of network round trips, increasing write throughput.
11

BULK INSERT

13       First  we need to fetch a bulk operation handle from the mongoc_collec‐
14       tion_t.
15
16          mongoc_bulk_operation_t *bulk =
17             mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
18
19       We can now start inserting documents to the bulk operation. These  will
20       be buffered until we execute the operation.
21
22       The  bulk operation will coalesce insertions as a single batch for each
23       consecutive call  to  mongoc_bulk_operation_insert().  This  creates  a
24       pipelined effect when possible.
25
26       To  execute  the  bulk  operation  and  receive the result we call mon‐
27       goc_bulk_operation_execute().
28
29       bulk1.c
30
31          #include <assert.h>
32          #include <mongoc/mongoc.h>
33          #include <stdio.h>
34
35          static void
36          bulk1 (mongoc_collection_t *collection)
37          {
38             mongoc_bulk_operation_t *bulk;
39             bson_error_t error;
40             bson_t *doc;
41             bson_t reply;
42             char *str;
43             bool ret;
44             int i;
45
46             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
47
48             for (i = 0; i < 10000; i++) {
49                doc = BCON_NEW ("i", BCON_INT32 (i));
50                mongoc_bulk_operation_insert (bulk, doc);
51                bson_destroy (doc);
52             }
53
54             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
55
56             str = bson_as_canonical_extended_json (&reply, NULL);
57             printf ("%s\n", str);
58             bson_free (str);
59
60             if (!ret) {
61                fprintf (stderr, "Error: %s\n", error.message);
62             }
63
64             bson_destroy (&reply);
65             mongoc_bulk_operation_destroy (bulk);
66          }
67
68          int
69          main (int argc, char *argv[])
70          {
71             mongoc_client_t *client;
72             mongoc_collection_t *collection;
73             const char *uri_string = "mongodb://localhost/?appname=bulk1-example";
74             mongoc_uri_t *uri;
75             bson_error_t error;
76
77             mongoc_init ();
78
79             uri = mongoc_uri_new_with_error (uri_string, &error);
80             if (!uri) {
81                fprintf (stderr,
82                         "failed to parse URI: %s\n"
83                         "error message:       %s\n",
84                         uri_string,
85                         error.message);
86                return EXIT_FAILURE;
87             }
88
89             client = mongoc_client_new_from_uri (uri);
90             if (!client) {
91                return EXIT_FAILURE;
92             }
93
94             mongoc_client_set_error_api (client, 2);
95             collection = mongoc_client_get_collection (client, "test", "test");
96
97             bulk1 (collection);
98
99             mongoc_uri_destroy (uri);
100             mongoc_collection_destroy (collection);
101             mongoc_client_destroy (client);
102
103             mongoc_cleanup ();
104
105             return EXIT_SUCCESS;
106          }
107
108
109       Example reply document:
110
111          {"nInserted"   : 10000,
112           "nMatched"    : 0,
113           "nModified"   : 0,
114           "nRemoved"    : 0,
115           "nUpserted"   : 0,
116           "writeErrors" : []
117           "writeConcernErrors" : [] }
118

MIXED BULK WRITE OPERATIONS

120       MongoDB C driver also supports executing mixed bulk write operations. A
121       batch of insert, update, and remove operations can be executed together
122       using the bulk write operations API.
123

ORDERED BULK WRITE OPERATIONS

125       Ordered bulk write operations are batched and sent to the server in the
126       order  provided  for serial execution. The reply document describes the
127       type and count of operations performed.
128
129       bulk2.c
130
131          #include <assert.h>
132          #include <mongoc/mongoc.h>
133          #include <stdio.h>
134
135          static void
136          bulk2 (mongoc_collection_t *collection)
137          {
138             mongoc_bulk_operation_t *bulk;
139             bson_error_t error;
140             bson_t *query;
141             bson_t *doc;
142             bson_t *opts;
143             bson_t reply;
144             char *str;
145             bool ret;
146             int i;
147
148             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
149
150             /* Remove everything */
151             query = bson_new ();
152             mongoc_bulk_operation_remove (bulk, query);
153             bson_destroy (query);
154
155             /* Add a few documents */
156             for (i = 1; i < 4; i++) {
157                doc = BCON_NEW ("_id", BCON_INT32 (i));
158                mongoc_bulk_operation_insert (bulk, doc);
159                bson_destroy (doc);
160             }
161
162             /* {_id: 1} => {$set: {foo: "bar"}} */
163             query = BCON_NEW ("_id", BCON_INT32 (1));
164             doc = BCON_NEW ("$set", "{", "foo", BCON_UTF8 ("bar"), "}");
165             mongoc_bulk_operation_update_many_with_opts (bulk, query, doc, NULL, &error);
166             bson_destroy (query);
167             bson_destroy (doc);
168
169             /* {_id: 4} => {'$inc': {'j': 1}} (upsert) */
170             opts = BCON_NEW ("upsert", BCON_BOOL (true));
171             query = BCON_NEW ("_id", BCON_INT32 (4));
172             doc = BCON_NEW ("$inc", "{", "j", BCON_INT32 (1), "}");
173             mongoc_bulk_operation_update_many_with_opts (bulk, query, doc, opts, &error);
174             bson_destroy (query);
175             bson_destroy (doc);
176             bson_destroy (opts);
177
178             /* replace {j:1} with {j:2} */
179             query = BCON_NEW ("j", BCON_INT32 (1));
180             doc = BCON_NEW ("j", BCON_INT32 (2));
181             mongoc_bulk_operation_replace_one_with_opts (bulk, query, doc, NULL, &error);
182             bson_destroy (query);
183             bson_destroy (doc);
184
185             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
186
187             str = bson_as_canonical_extended_json (&reply, NULL);
188             printf ("%s\n", str);
189             bson_free (str);
190
191             if (!ret) {
192                printf ("Error: %s\n", error.message);
193             }
194
195             bson_destroy (&reply);
196             mongoc_bulk_operation_destroy (bulk);
197          }
198
199          int
200          main (int argc, char *argv[])
201          {
202             mongoc_client_t *client;
203             mongoc_collection_t *collection;
204             const char *uri_string = "mongodb://localhost/?appname=bulk2-example";
205             mongoc_uri_t *uri;
206             bson_error_t error;
207
208             mongoc_init ();
209
210             uri = mongoc_uri_new_with_error (uri_string, &error);
211             if (!uri) {
212                fprintf (stderr,
213                         "failed to parse URI: %s\n"
214                         "error message:       %s\n",
215                         uri_string,
216                         error.message);
217                return EXIT_FAILURE;
218             }
219
220             client = mongoc_client_new_from_uri (uri);
221             if (!client) {
222                return EXIT_FAILURE;
223             }
224
225             mongoc_client_set_error_api (client, 2);
226             collection = mongoc_client_get_collection (client, "test", "test");
227
228             bulk2 (collection);
229
230             mongoc_uri_destroy (uri);
231             mongoc_collection_destroy (collection);
232             mongoc_client_destroy (client);
233
234             mongoc_cleanup ();
235
236             return EXIT_SUCCESS;
237          }
238
239
240       Example reply document:
241
242          { "nInserted"   : 3,
243            "nMatched"    : 2,
244            "nModified"   : 2,
245            "nRemoved"    : 10000,
246            "nUpserted"   : 1,
247            "upserted"    : [{"index" : 5, "_id" : 4}],
248            "writeErrors" : []
249            "writeConcernErrors" : [] }
250
251       The index field in the upserted array is the 0-based index of  the  up‐
252       sert  operation;  in  this  example, the sixth operation of the overall
253       bulk operation was an upsert, so its index is 5.
254

UNORDERED BULK WRITE OPERATIONS

256       Unordered bulk write operations are batched and sent to the  server  in
257       arbitrary order where they may be executed in parallel. Any errors that
258       occur are reported after all operations are attempted.
259
260       In the next example the first and third  operations  fail  due  to  the
261       unique  constraint  on  _id. Since we are doing unordered execution the
262       second and fourth operations succeed.
263
264       bulk3.c
265
266          #include <assert.h>
267          #include <mongoc/mongoc.h>
268          #include <stdio.h>
269
270          static void
271          bulk3 (mongoc_collection_t *collection)
272          {
273             bson_t opts = BSON_INITIALIZER;
274             mongoc_bulk_operation_t *bulk;
275             bson_error_t error;
276             bson_t *query;
277             bson_t *doc;
278             bson_t reply;
279             char *str;
280             bool ret;
281
282             /* false indicates unordered */
283             BSON_APPEND_BOOL (&opts, "ordered", false);
284             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, &opts);
285             bson_destroy (&opts);
286
287             /* Add a document */
288             doc = BCON_NEW ("_id", BCON_INT32 (1));
289             mongoc_bulk_operation_insert (bulk, doc);
290             bson_destroy (doc);
291
292             /* remove {_id: 2} */
293             query = BCON_NEW ("_id", BCON_INT32 (2));
294             mongoc_bulk_operation_remove_one (bulk, query);
295             bson_destroy (query);
296
297             /* insert {_id: 3} */
298             doc = BCON_NEW ("_id", BCON_INT32 (3));
299             mongoc_bulk_operation_insert (bulk, doc);
300             bson_destroy (doc);
301
302             /* replace {_id:4} {'i': 1} */
303             query = BCON_NEW ("_id", BCON_INT32 (4));
304             doc = BCON_NEW ("i", BCON_INT32 (1));
305             mongoc_bulk_operation_replace_one (bulk, query, doc, false);
306             bson_destroy (query);
307             bson_destroy (doc);
308
309             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
310
311             str = bson_as_canonical_extended_json (&reply, NULL);
312             printf ("%s\n", str);
313             bson_free (str);
314
315             if (!ret) {
316                printf ("Error: %s\n", error.message);
317             }
318
319             bson_destroy (&reply);
320             mongoc_bulk_operation_destroy (bulk);
321             bson_destroy (&opts);
322          }
323
324          int
325          main (int argc, char *argv[])
326          {
327             mongoc_client_t *client;
328             mongoc_collection_t *collection;
329             const char *uri_string = "mongodb://localhost/?appname=bulk3-example";
330             mongoc_uri_t *uri;
331             bson_error_t error;
332
333             mongoc_init ();
334
335             uri = mongoc_uri_new_with_error (uri_string, &error);
336             if (!uri) {
337                fprintf (stderr,
338                         "failed to parse URI: %s\n"
339                         "error message:       %s\n",
340                         uri_string,
341                         error.message);
342                return EXIT_FAILURE;
343             }
344
345             client = mongoc_client_new_from_uri (uri);
346             if (!client) {
347                return EXIT_FAILURE;
348             }
349
350             mongoc_client_set_error_api (client, 2);
351             collection = mongoc_client_get_collection (client, "test", "test");
352
353             bulk3 (collection);
354
355             mongoc_uri_destroy (uri);
356             mongoc_collection_destroy (collection);
357             mongoc_client_destroy (client);
358
359             mongoc_cleanup ();
360
361             return EXIT_SUCCESS;
362          }
363
364
365       Example reply document:
366
367          { "nInserted"    : 0,
368            "nMatched"     : 1,
369            "nModified"    : 1,
370            "nRemoved"     : 1,
371            "nUpserted"    : 0,
372            "writeErrors"  : [
373              { "index"  : 0,
374                "code"   : 11000,
375                "errmsg" : "E11000 duplicate key error index: test.test.$_id_ dup key: { : 1 }" },
376              { "index"  : 2,
377                "code"   : 11000,
378                "errmsg" : "E11000 duplicate key error index: test.test.$_id_ dup key: { : 3 }" } ],
379            "writeConcernErrors" : [] }
380
381          Error: E11000 duplicate key error index: test.test.$_id_ dup key: { : 1 }
382
383       The bson_error_t domain is MONGOC_ERROR_COMMAND and its code is 11000.
384

BULK OPERATION BYPASSING DOCUMENT VALIDATION

386       This feature is only available when using MongoDB 3.2 and later.
387
388       By default bulk operations are validated against the schema, if any  is
389       defined.  In  certain  cases  however it may be necessary to bypass the
390       document validation.
391
392       bulk5.c
393
394          #include <assert.h>
395          #include <mongoc/mongoc.h>
396          #include <stdio.h>
397
398          static void
399          bulk5_fail (mongoc_collection_t *collection)
400          {
401             mongoc_bulk_operation_t *bulk;
402             bson_error_t error;
403             bson_t *doc;
404             bson_t reply;
405             char *str;
406             bool ret;
407
408             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
409
410             /* Two inserts */
411             doc = BCON_NEW ("_id", BCON_INT32 (31));
412             mongoc_bulk_operation_insert (bulk, doc);
413             bson_destroy (doc);
414
415             doc = BCON_NEW ("_id", BCON_INT32 (32));
416             mongoc_bulk_operation_insert (bulk, doc);
417             bson_destroy (doc);
418
419             /* The above documents do not comply to the schema validation rules
420              * we created previously, so this will result in an error */
421             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
422
423             str = bson_as_canonical_extended_json (&reply, NULL);
424             printf ("%s\n", str);
425             bson_free (str);
426
427             if (!ret) {
428                printf ("Error: %s\n", error.message);
429             }
430
431             bson_destroy (&reply);
432             mongoc_bulk_operation_destroy (bulk);
433          }
434
435          static void
436          bulk5_success (mongoc_collection_t *collection)
437          {
438             mongoc_bulk_operation_t *bulk;
439             bson_error_t error;
440             bson_t *doc;
441             bson_t reply;
442             char *str;
443             bool ret;
444
445             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
446
447             /* Allow this document to bypass document validation.
448              * NOTE: When authentication is enabled, the authenticated user must have
449              * either the "dbadmin" or "restore" roles to bypass document validation */
450             mongoc_bulk_operation_set_bypass_document_validation (bulk, true);
451
452             /* Two inserts */
453             doc = BCON_NEW ("_id", BCON_INT32 (31));
454             mongoc_bulk_operation_insert (bulk, doc);
455             bson_destroy (doc);
456
457             doc = BCON_NEW ("_id", BCON_INT32 (32));
458             mongoc_bulk_operation_insert (bulk, doc);
459             bson_destroy (doc);
460
461             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
462
463             str = bson_as_canonical_extended_json (&reply, NULL);
464             printf ("%s\n", str);
465             bson_free (str);
466
467             if (!ret) {
468                printf ("Error: %s\n", error.message);
469             }
470
471             bson_destroy (&reply);
472             mongoc_bulk_operation_destroy (bulk);
473          }
474
475          int
476          main (int argc, char *argv[])
477          {
478             bson_t *options;
479             bson_error_t error;
480             mongoc_client_t *client;
481             mongoc_collection_t *collection;
482             mongoc_database_t *database;
483             const char *uri_string = "mongodb://localhost/?appname=bulk5-example";
484             mongoc_uri_t *uri;
485
486             mongoc_init ();
487
488             uri = mongoc_uri_new_with_error (uri_string, &error);
489             if (!uri) {
490                fprintf (stderr,
491                         "failed to parse URI: %s\n"
492                         "error message:       %s\n",
493                         uri_string,
494                         error.message);
495                return EXIT_FAILURE;
496             }
497
498             client = mongoc_client_new_from_uri (uri);
499             if (!client) {
500                return EXIT_FAILURE;
501             }
502
503             mongoc_client_set_error_api (client, 2);
504             database = mongoc_client_get_database (client, "testasdf");
505
506             /* Create schema validator */
507             options = BCON_NEW (
508                "validator", "{", "number", "{", "$gte", BCON_INT32 (5), "}", "}");
509             collection =
510                mongoc_database_create_collection (database, "collname", options, &error);
511
512             if (collection) {
513                bulk5_fail (collection);
514                bulk5_success (collection);
515                mongoc_collection_destroy (collection);
516             } else {
517                fprintf (stderr, "Couldn't create collection: '%s'\n", error.message);
518             }
519
520             bson_free (options);
521             mongoc_uri_destroy (uri);
522             mongoc_database_destroy (database);
523             mongoc_client_destroy (client);
524
525             mongoc_cleanup ();
526
527             return EXIT_SUCCESS;
528          }
529
530
531       Running the above example will result in:
532
533          { "nInserted" : 0,
534            "nMatched" : 0,
535            "nModified" : 0,
536            "nRemoved" : 0,
537            "nUpserted" : 0,
538            "writeErrors" : [
539              { "index" : 0,
540                "code" : 121,
541                "errmsg" : "Document failed validation" } ] }
542
543          Error: Document failed validation
544
545          { "nInserted" : 2,
546            "nMatched" : 0,
547            "nModified" : 0,
548            "nRemoved" : 0,
549            "nUpserted" : 0,
550            "writeErrors" : [] }
551
552       The bson_error_t domain is MONGOC_ERROR_COMMAND.
553

BULK OPERATION WRITE CONCERNS

555       By default bulk operations are executed with the write_concern  of  the
556       collection  they  are  executed  against. A custom write concern can be
557       passed   to   the   mongoc_collection_create_bulk_operation_with_opts()
558       method. Write concern errors (e.g. wtimeout) will be reported after all
559       operations are attempted, regardless of execution order.
560
561       bulk4.c
562
563          #include <assert.h>
564          #include <mongoc/mongoc.h>
565          #include <stdio.h>
566
567          static void
568          bulk4 (mongoc_collection_t *collection)
569          {
570             bson_t opts = BSON_INITIALIZER;
571             mongoc_write_concern_t *wc;
572             mongoc_bulk_operation_t *bulk;
573             bson_error_t error;
574             bson_t *doc;
575             bson_t reply;
576             char *str;
577             bool ret;
578
579             wc = mongoc_write_concern_new ();
580             mongoc_write_concern_set_w (wc, 4);
581             mongoc_write_concern_set_wtimeout_int64 (wc, 100); /* milliseconds */
582             mongoc_write_concern_append (wc, &opts);
583
584             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, &opts);
585
586             /* Two inserts */
587             doc = BCON_NEW ("_id", BCON_INT32 (10));
588             mongoc_bulk_operation_insert (bulk, doc);
589             bson_destroy (doc);
590
591             doc = BCON_NEW ("_id", BCON_INT32 (11));
592             mongoc_bulk_operation_insert (bulk, doc);
593             bson_destroy (doc);
594
595             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
596
597             str = bson_as_canonical_extended_json (&reply, NULL);
598             printf ("%s\n", str);
599             bson_free (str);
600
601             if (!ret) {
602                printf ("Error: %s\n", error.message);
603             }
604
605             bson_destroy (&reply);
606             mongoc_bulk_operation_destroy (bulk);
607             mongoc_write_concern_destroy (wc);
608             bson_destroy (&opts);
609          }
610
611          int
612          main (int argc, char *argv[])
613          {
614             mongoc_client_t *client;
615             mongoc_collection_t *collection;
616             const char *uri_string = "mongodb://localhost/?appname=bulk4-example";
617             mongoc_uri_t *uri;
618             bson_error_t error;
619
620             mongoc_init ();
621
622             uri = mongoc_uri_new_with_error (uri_string, &error);
623             if (!uri) {
624                fprintf (stderr,
625                         "failed to parse URI: %s\n"
626                         "error message:       %s\n",
627                         uri_string,
628                         error.message);
629                return EXIT_FAILURE;
630             }
631
632             client = mongoc_client_new_from_uri (uri);
633             if (!client) {
634                return EXIT_FAILURE;
635             }
636
637             mongoc_client_set_error_api (client, 2);
638             collection = mongoc_client_get_collection (client, "test", "test");
639
640             bulk4 (collection);
641
642             mongoc_uri_destroy (uri);
643             mongoc_collection_destroy (collection);
644             mongoc_client_destroy (client);
645
646             mongoc_cleanup ();
647
648             return EXIT_SUCCESS;
649          }
650
651
652       Example reply document and error message:
653
654          { "nInserted"    : 2,
655            "nMatched"     : 0,
656            "nModified"    : 0,
657            "nRemoved"     : 0,
658            "nUpserted"    : 0,
659            "writeErrors"  : [],
660            "writeConcernErrors" : [
661              { "code"   : 64,
662                "errmsg" : "waiting for replication timed out" }
663          ] }
664
665          Error: waiting for replication timed out
666
667       The bson_error_t domain  is  MONGOC_ERROR_WRITE_CONCERN  if  there  are
668       write  concern errors and no write errors. Write errors indicate failed
669       operations, so they take precedence over write  concern  errors,  which
670       mean merely that the write concern is not satisfied yet.
671

SETTING COLLATION ORDER

673       This feature is only available when using MongoDB 3.4 and later.
674
675       bulk-collation.c
676
677          #include <mongoc/mongoc.h>
678          #include <stdio.h>
679
680          static void
681          bulk_collation (mongoc_collection_t *collection)
682          {
683             mongoc_bulk_operation_t *bulk;
684             bson_t *opts;
685             bson_t *doc;
686             bson_t *selector;
687             bson_t *update;
688             bson_error_t error;
689             bson_t reply;
690             char *str;
691             uint32_t ret;
692
693             /* insert {_id: "one"} and {_id: "One"} */
694             bulk = mongoc_collection_create_bulk_operation_with_opts (
695                collection, NULL);
696             doc = BCON_NEW ("_id", BCON_UTF8 ("one"));
697             mongoc_bulk_operation_insert (bulk, doc);
698             bson_destroy (doc);
699
700             doc = BCON_NEW ("_id", BCON_UTF8 ("One"));
701             mongoc_bulk_operation_insert (bulk, doc);
702             bson_destroy (doc);
703
704             /* "One" normally sorts before "one"; make "one" come first */
705             opts = BCON_NEW ("collation",
706                              "{",
707                              "locale",
708                              BCON_UTF8 ("en_US"),
709                              "caseFirst",
710                              BCON_UTF8 ("lower"),
711                              "}");
712
713             /* set x=1 on the document with _id "One", which now sorts after "one" */
714             update = BCON_NEW ("$set", "{", "x", BCON_INT64 (1), "}");
715             selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}");
716             mongoc_bulk_operation_update_one_with_opts (
717                bulk, selector, update, opts, &error);
718
719             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
720
721             str = bson_as_canonical_extended_json (&reply, NULL);
722             printf ("%s\n", str);
723             bson_free (str);
724
725             if (!ret) {
726                printf ("Error: %s\n", error.message);
727             }
728
729             bson_destroy (&reply);
730             bson_destroy (update);
731             bson_destroy (selector);
732             bson_destroy (opts);
733             mongoc_bulk_operation_destroy (bulk);
734          }
735
736          int
737          main (int argc, char *argv[])
738          {
739             mongoc_client_t *client;
740             mongoc_collection_t *collection;
741             const char *uri_string = "mongodb://localhost/?appname=bulk-collation";
742             mongoc_uri_t *uri;
743             bson_error_t error;
744
745             mongoc_init ();
746
747             uri = mongoc_uri_new_with_error (uri_string, &error);
748             if (!uri) {
749                fprintf (stderr,
750                         "failed to parse URI: %s\n"
751                         "error message:       %s\n",
752                         uri_string,
753                         error.message);
754                return EXIT_FAILURE;
755             }
756
757             client = mongoc_client_new_from_uri (uri);
758             if (!client) {
759                return EXIT_FAILURE;
760             }
761
762             mongoc_client_set_error_api (client, 2);
763             collection = mongoc_client_get_collection (client, "db", "collection");
764             bulk_collation (collection);
765
766             mongoc_uri_destroy (uri);
767             mongoc_collection_destroy (collection);
768             mongoc_client_destroy (client);
769
770             mongoc_cleanup ();
771
772             return EXIT_SUCCESS;
773          }
774
775
776       Running the above example will result in:
777
778          { "nInserted" : 2,
779             "nMatched" : 1,
780             "nModified" : 1,
781             "nRemoved" : 0,
782             "nUpserted" : 0,
783             "writeErrors" : [  ]
784          }
785

UNACKNOWLEDGED BULK WRITES

787       Set  "w"  to  zero  for an unacknowledged write. The driver sends unac‐
788       knowledged writes using the legacy opcodes  OP_INSERT,  OP_UPDATE,  and
789       OP_DELETE.
790
791       bulk6.c
792
793          #include <mongoc/mongoc.h>
794          #include <stdio.h>
795
796          static void
797          bulk6 (mongoc_collection_t *collection)
798          {
799             bson_t opts = BSON_INITIALIZER;
800             mongoc_write_concern_t *wc;
801             mongoc_bulk_operation_t *bulk;
802             bson_error_t error;
803             bson_t *doc;
804             bson_t *selector;
805             bson_t reply;
806             char *str;
807             bool ret;
808
809             wc = mongoc_write_concern_new ();
810             mongoc_write_concern_set_w (wc, 0);
811             mongoc_write_concern_append (wc, &opts);
812
813             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, &opts);
814
815             doc = BCON_NEW ("_id", BCON_INT32 (10));
816             mongoc_bulk_operation_insert (bulk, doc);
817             bson_destroy (doc);
818
819             selector = BCON_NEW ("_id", BCON_INT32 (11));
820             mongoc_bulk_operation_remove_one (bulk, selector);
821             bson_destroy (selector);
822
823             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
824
825             str = bson_as_canonical_extended_json (&reply, NULL);
826             printf ("%s\n", str);
827             bson_free (str);
828
829             if (!ret) {
830                printf ("Error: %s\n", error.message);
831             }
832
833             bson_destroy (&reply);
834             mongoc_bulk_operation_destroy (bulk);
835             mongoc_write_concern_destroy (wc);
836             bson_destroy (&opts);
837          }
838
839          int
840          main (int argc, char *argv[])
841          {
842             mongoc_client_t *client;
843             mongoc_collection_t *collection;
844             const char *uri_string = "mongodb://localhost/?appname=bulk6-example";
845             mongoc_uri_t *uri;
846             bson_error_t error;
847
848             mongoc_init ();
849
850             uri = mongoc_uri_new_with_error (uri_string, &error);
851             if (!uri) {
852                fprintf (stderr,
853                         "failed to parse URI: %s\n"
854                         "error message:       %s\n",
855                         uri_string,
856                         error.message);
857                return EXIT_FAILURE;
858             }
859
860             client = mongoc_client_new_from_uri (uri);
861             if (!client) {
862                return EXIT_FAILURE;
863             }
864
865             mongoc_client_set_error_api (client, 2);
866             collection = mongoc_client_get_collection (client, "test", "test");
867
868             bulk6 (collection);
869
870             mongoc_uri_destroy (uri);
871             mongoc_collection_destroy (collection);
872             mongoc_client_destroy (client);
873
874             mongoc_cleanup ();
875
876             return EXIT_SUCCESS;
877          }
878
879
880       The reply document is empty:
881
882          { }
883

FURTHER READING

885       See the Driver Bulk API Spec, which describes bulk write operations for
886       all MongoDB drivers.
887

AUTHOR

889       MongoDB, Inc
890
892       2017-present, MongoDB, Inc
893
894
895
896
8971.20.0                           Nov 18, 2021                   MONGOC_BULK(3)
Impressum