1 /*
2 
3 Demonstrates how to prepare options for mongoc_client_read_command_with_opts and
4 mongoc_client_write_command_with_opts. First it calls "cloneCollectionAsCapped"
5 command with "writeConcern" option, then "distinct" command with "collation" and
6 "readConcern" options,
7 
8 Start a MongoDB 3.4 replica set with --enableMajorityReadConcern and insert two
9 documents:
10 
11 $ mongo
12 MongoDB Enterprise replset:PRIMARY> db.my_collection.insert({x: 1, y: "One"})
13 WriteResult({ "nInserted" : 1 })
14 MongoDB Enterprise replset:PRIMARY> db.my_collection.insert({x: 2, y: "Two"})
15 WriteResult({ "nInserted" : 1 })
16 
17 Build and run the example:
18 
19 gcc example-command-with-opts.c -o example-command-with-opts $(pkg-config
20 --cflags --libs libmongoc-1.0)
21 ./example-command-with-opts [CONNECTION_STRING]
22 cloneCollectionAsCapped: { "ok" : 1 }
23 distinct: { "values" : [ 1, 2 ], "ok" : 1 }
24 
25 */
26 
27 #include <mongoc.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 int
main(int argc,char * argv[])32 main (int argc, char *argv[])
33 {
34    mongoc_client_t *client;
35    const char *uristr = "mongodb://127.0.0.1/?appname=client-example";
36    bson_t *cmd;
37    bson_t *opts;
38    mongoc_write_concern_t *write_concern;
39    mongoc_read_prefs_t *read_prefs;
40    mongoc_read_concern_t *read_concern;
41    bson_t reply;
42    bson_error_t error;
43    char *json;
44 
45    mongoc_init ();
46 
47    if (argc > 1) {
48       uristr = argv[1];
49    }
50 
51    client = mongoc_client_new (uristr);
52 
53    if (!client) {
54       fprintf (stderr, "Failed to parse URI.\n");
55       return EXIT_FAILURE;
56    }
57 
58    mongoc_client_set_error_api (client, 2);
59 
60    cmd = BCON_NEW ("cloneCollectionAsCapped",
61                    BCON_UTF8 ("my_collection"),
62                    "toCollection",
63                    BCON_UTF8 ("my_capped_collection"),
64                    "size",
65                    BCON_INT64 (1024 * 1024));
66 
67    /* include write concern "majority" in command options */
68    write_concern = mongoc_write_concern_new ();
69    mongoc_write_concern_set_wmajority (write_concern, 10000 /* wtimeoutMS */);
70    opts = bson_new ();
71    mongoc_write_concern_append (write_concern, opts);
72 
73    if (mongoc_client_write_command_with_opts (
74           client, "test", cmd, opts, &reply, &error)) {
75       json = bson_as_canonical_extended_json (&reply, NULL);
76       printf ("cloneCollectionAsCapped: %s\n", json);
77       bson_free (json);
78    } else {
79       fprintf (stderr, "cloneCollectionAsCapped: %s\n", error.message);
80    }
81 
82    bson_free (cmd);
83    bson_free (opts);
84 
85    /* distinct values of "x" in "my_collection" where "y" sorts after "one" */
86    cmd = BCON_NEW ("distinct",
87                    BCON_UTF8 ("my_collection"),
88                    "key",
89                    BCON_UTF8 ("x"),
90                    "query",
91                    "{",
92                    "y",
93                    "{",
94                    "$gt",
95                    BCON_UTF8 ("one"),
96                    "}",
97                    "}");
98 
99    read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);
100 
101    /* "One" normally sorts before "one"; make "One" sort after "one" */
102    opts = BCON_NEW ("collation",
103                     "{",
104                     "locale",
105                     BCON_UTF8 ("en_US"),
106                     "caseFirst",
107                     BCON_UTF8 ("lower"),
108                     "}");
109 
110    /* add a read concern to "opts" */
111    read_concern = mongoc_read_concern_new ();
112    mongoc_read_concern_set_level (read_concern,
113                                   MONGOC_READ_CONCERN_LEVEL_MAJORITY);
114 
115    mongoc_read_concern_append (read_concern, opts);
116 
117    if (mongoc_client_read_command_with_opts (
118           client, "test", cmd, read_prefs, opts, &reply, &error)) {
119       json = bson_as_canonical_extended_json (&reply, NULL);
120       printf ("distinct: %s\n", json);
121       bson_free (json);
122    } else {
123       fprintf (stderr, "distinct: %s\n", error.message);
124    }
125 
126    bson_destroy (cmd);
127    bson_destroy (opts);
128    bson_destroy (&reply);
129    mongoc_read_prefs_destroy (read_prefs);
130    mongoc_read_concern_destroy (read_concern);
131    mongoc_write_concern_destroy (write_concern);
132    mongoc_client_destroy (client);
133 
134    mongoc_cleanup ();
135 
136    return EXIT_SUCCESS;
137 }
138