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

MIXED BULK WRITE OPERATIONS

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

ORDERED BULK WRITE OPERATIONS

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

UNORDERED BULK WRITE OPERATIONS

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

BULK OPERATION BYPASSING DOCUMENT VALIDATION

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

BULK OPERATION WRITE CONCERNS

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

SETTING COLLATION ORDER

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

UNACKNOWLEDGED BULK WRITES

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

FURTHER READING

873       See the Driver Bulk API Spec, which describes bulk write operations for
874       all MongoDB drivers.
875

AUTHOR

877       MongoDB, Inc
878
880       2017-present, MongoDB, Inc
881
882
883
884
8851.16.2                           Feb 25, 2020                   MONGOC_BULK(3)
Impressum