1 #include <assert.h>
2 #include <bcon.h>
3 #include <mongoc.h>
4 #include <stdio.h>
5 
6 static void
bulk3(mongoc_collection_t * collection)7 bulk3 (mongoc_collection_t *collection)
8 {
9    mongoc_bulk_operation_t *bulk;
10    bson_error_t error;
11    bson_t *query;
12    bson_t *doc;
13    bson_t reply;
14    char *str;
15    bool ret;
16 
17    /* false indicates unordered */
18    bulk = mongoc_collection_create_bulk_operation (collection, false, NULL);
19 
20    /* Add a document */
21    doc = BCON_NEW ("_id", BCON_INT32 (1));
22    mongoc_bulk_operation_insert (bulk, doc);
23    bson_destroy (doc);
24 
25    /* remove {_id: 2} */
26    query = BCON_NEW ("_id", BCON_INT32 (2));
27    mongoc_bulk_operation_remove_one (bulk, query);
28    bson_destroy (query);
29 
30    /* insert {_id: 3} */
31    doc = BCON_NEW ("_id", BCON_INT32 (3));
32    mongoc_bulk_operation_insert (bulk, doc);
33    bson_destroy (doc);
34 
35    /* replace {_id:4} {'i': 1} */
36    query = BCON_NEW ("_id", BCON_INT32 (4));
37    doc = BCON_NEW ("i", BCON_INT32 (1));
38    mongoc_bulk_operation_replace_one (bulk, query, doc, false);
39    bson_destroy (query);
40    bson_destroy (doc);
41 
42    ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
43 
44    str = bson_as_canonical_extended_json (&reply, NULL);
45    printf ("%s\n", str);
46    bson_free (str);
47 
48    if (!ret) {
49       printf ("Error: %s\n", error.message);
50    }
51 
52    bson_destroy (&reply);
53    mongoc_bulk_operation_destroy (bulk);
54 }
55 
56 int
main(int argc,char * argv[])57 main (int argc, char *argv[])
58 {
59    mongoc_client_t *client;
60    mongoc_collection_t *collection;
61 
62    mongoc_init ();
63 
64    client = mongoc_client_new ("mongodb://localhost/?appname=bulk3-example");
65    mongoc_client_set_error_api (client, 2);
66    collection = mongoc_client_get_collection (client, "test", "test");
67 
68    bulk3 (collection);
69 
70    mongoc_collection_destroy (collection);
71    mongoc_client_destroy (client);
72 
73    mongoc_cleanup ();
74 
75    return 0;
76 }
77