1MONGOC_BULK(3)                 MongoDB C Driver                 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().  bulk1.c.INDENT 0.0
28
29          #include <assert.h>
30          #include <bson/bcon.h>
31          #include <mongoc/mongoc.h>
32          #include <stdio.h>
33
34          static void
35          bulk1 (mongoc_collection_t *collection)
36          {
37             mongoc_bulk_operation_t *bulk;
38             bson_error_t error;
39             bson_t *doc;
40             bson_t reply;
41             char *str;
42             bool ret;
43             int i;
44
45             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
46
47             for (i = 0; i < 10000; i++) {
48                doc = BCON_NEW ("i", BCON_INT32 (i));
49                mongoc_bulk_operation_insert (bulk, doc);
50                bson_destroy (doc);
51             }
52
53             ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
54
55             str = bson_as_canonical_extended_json (&reply, NULL);
56             printf ("%s\n", str);
57             bson_free (str);
58
59             if (!ret) {
60                fprintf (stderr, "Error: %s\n", error.message);
61             }
62
63             bson_destroy (&reply);
64             mongoc_bulk_operation_destroy (bulk);
65          }
66
67          int
68          main (int argc, char *argv[])
69          {
70             mongoc_client_t *client;
71             mongoc_collection_t *collection;
72             const char *uri_string = "mongodb://localhost/?appname=bulk1-example";
73             mongoc_uri_t *uri;
74             bson_error_t error;
75
76             mongoc_init ();
77
78             uri = mongoc_uri_new_with_error (uri_string, &error);
79             if (!uri) {
80                fprintf (stderr,
81                         "failed to parse URI: %s\n"
82                         "error message:       %s\n",
83                         uri_string,
84                         error.message);
85                return EXIT_FAILURE;
86             }
87
88             client = mongoc_client_new_from_uri (uri);
89             if (!client) {
90                return EXIT_FAILURE;
91             }
92
93             mongoc_client_set_error_api (client, 2);
94             collection = mongoc_client_get_collection (client, "test", "test");
95
96             bulk1 (collection);
97
98             mongoc_uri_destroy (uri);
99             mongoc_collection_destroy (collection);
100             mongoc_client_destroy (client);
101
102             mongoc_cleanup ();
103
104             return EXIT_SUCCESS;
105          }
106
107
108Example reply document:
109
110          {"nInserted"   : 10000,
111           "nMatched"    : 0,
112           "nModified"   : 0,
113           "nRemoved"    : 0,
114           "nUpserted"   : 0,
115           "writeErrors" : []
116           "writeConcernErrors" : [] }
117

MIXED BULK WRITE OPERATIONS

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

ORDERED BULK WRITE OPERATIONS

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

UNORDERED BULK WRITE OPERATIONS

254       Unordered bulk write operations are batched and sent to the  server  in
255       arbitrary order where they may be executed in parallel. Any errors that
256       occur are reported after all operations are attempted.
257
258       In the next example the first and third  operations  fail  due  to  the
259       unique  constraint  on  _id. Since we are doing unordered execution the
260       second and fourth operations succeed.  bulk3.c.INDENT 0.0
261
262          #include <assert.h>
263          #include <bson/bcon.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 (int argc, char *argv[])
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
362Example 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

BULK OPERATION BYPASSING DOCUMENT VALIDATION

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

BULK OPERATION WRITE CONCERNS

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

SETTING COLLATION ORDER

669       This feature is only  available  when  using  MongoDB  3.4  and  later.
670       bulk-collation.c.INDENT 0.0
671
672          #include <bson/bcon.h>
673          #include <mongoc/mongoc.h>
674          #include <stdio.h>
675
676          static void
677          bulk_collation (mongoc_collection_t *collection)
678          {
679             mongoc_bulk_operation_t *bulk;
680             bson_t *opts;
681             bson_t *doc;
682             bson_t *selector;
683             bson_t *update;
684             bson_error_t error;
685             bson_t reply;
686             char *str;
687             uint32_t ret;
688
689             /* insert {_id: "one"} and {_id: "One"} */
690             bulk = mongoc_collection_create_bulk_operation_with_opts (
691                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 (int argc, char *argv[])
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
772Running 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

UNACKNOWLEDGED BULK WRITES

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

FURTHER READING

880       See the Driver Bulk API Spec, which describes bulk write operations for
881       all MongoDB drivers.
882

AUTHOR

884       MongoDB, Inc
885
887       2017-present, MongoDB, Inc
888
889
890
891
8921.13.1                           Jan 24, 2019                   MONGOC_BULK(3)
Impressum