1MONGOC_COMMON_TASK_EXAMPLES(3) MongoDB C Driver MONGOC_COMMON_TASK_EXAMPLES(3)
2
3
4
6 mongoc_common_task_examples - Common Tasks
7
8 Drivers for some other languages provide helper functions to perform
9 certain common tasks. In the C Driver we must explicitly build commands
10 to send to the server.
11
12 This snippet contains example code for the explain, copydb and
13 cloneCollection commands.
14
16 First we'll write some code to insert sample data: doc-com‐
17 mon-insert.c.INDENT 0.0
18
19 /* Don't try to compile this file on its own. It's meant to be #included
20 by example code */
21
22 /* Insert some sample data */
23 bool
24 insert_data (mongoc_collection_t *collection)
25 {
26 mongoc_bulk_operation_t *bulk;
27 enum N { ndocs = 4 };
28 bson_t *docs[ndocs];
29 bson_error_t error;
30 int i = 0;
31 bool ret;
32
33 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
34
35 docs[0] = BCON_NEW ("x", BCON_DOUBLE (1.0), "tags", "[", "dog", "cat", "]");
36 docs[1] = BCON_NEW ("x", BCON_DOUBLE (2.0), "tags", "[", "cat", "]");
37 docs[2] = BCON_NEW (
38 "x", BCON_DOUBLE (2.0), "tags", "[", "mouse", "cat", "dog", "]");
39 docs[3] = BCON_NEW ("x", BCON_DOUBLE (3.0), "tags", "[", "]");
40
41 for (i = 0; i < ndocs; i++) {
42 mongoc_bulk_operation_insert (bulk, docs[i]);
43 bson_destroy (docs[i]);
44 docs[i] = NULL;
45 }
46
47 ret = mongoc_bulk_operation_execute (bulk, NULL, &error);
48
49 if (!ret) {
50 fprintf (stderr, "Error inserting data: %s\n", error.message);
51 }
52
53 mongoc_bulk_operation_destroy (bulk);
54 return ret;
55 }
56
57 /* A helper which we'll use a lot later on */
58 void
59 print_res (const bson_t *reply)
60 {
61 char *str;
62 BSON_ASSERT (reply);
63 str = bson_as_canonical_extended_json (reply, NULL);
64 printf ("%s\n", str);
65 bson_free (str);
66 }
67
68
70 This is how to use the explain command in MongoDB 3.2+:
71 explain.c.INDENT 0.0
72
73 bool
74 explain (mongoc_collection_t *collection)
75 {
76 bson_t *command;
77 bson_t reply;
78 bson_error_t error;
79 bool res;
80
81 command = BCON_NEW ("explain",
82 "{",
83 "find",
84 BCON_UTF8 (COLLECTION_NAME),
85 "filter",
86 "{",
87 "x",
88 BCON_INT32 (1),
89 "}",
90 "}");
91 res = mongoc_collection_command_simple (
92 collection, command, NULL, &reply, &error);
93 if (!res) {
94 fprintf (stderr, "Error with explain: %s\n", error.message);
95 goto cleanup;
96 }
97
98 /* Do something with the reply */
99 print_res (&reply);
100
101 cleanup:
102 bson_destroy (&reply);
103 bson_destroy (command);
104 return res;
105 }
106
107
109 This example requires two instances of mongo to be running.
110
111 Here's how to use the copydb command to copy a database from another
112 instance of MongoDB: copydb.c.INDENT 0.0
113
114 bool
115 copydb (mongoc_client_t *client, const char *other_host_and_port)
116 {
117 mongoc_database_t *admindb;
118 bson_t *command;
119 bson_t reply;
120 bson_error_t error;
121 bool res;
122
123 BSON_ASSERT (other_host_and_port);
124 /* Must do this from the admin db */
125 admindb = mongoc_client_get_database (client, "admin");
126
127 command = BCON_NEW ("copydb",
128 BCON_INT32 (1),
129 "fromdb",
130 BCON_UTF8 ("test"),
131 "todb",
132 BCON_UTF8 ("test2"),
133
134 /* If you want from a different host */
135 "fromhost",
136 BCON_UTF8 (other_host_and_port));
137 res =
138 mongoc_database_command_simple (admindb, command, NULL, &reply, &error);
139 if (!res) {
140 fprintf (stderr, "Error with copydb: %s\n", error.message);
141 goto cleanup;
142 }
143
144 /* Do something with the reply */
145 print_res (&reply);
146
147 cleanup:
148 bson_destroy (&reply);
149 bson_destroy (command);
150 mongoc_database_destroy (admindb);
151
152 return res;
153 }
154
155
157 This example requires two instances of mongo to be running.
158
159 Here's an example of the cloneCollection command to clone a collection
160 from another instance of MongoDB: clone-collection.c.INDENT 0.0
161
162 bool
163 clone_collection (mongoc_database_t *database, const char *other_host_and_port)
164 {
165 bson_t *command;
166 bson_t reply;
167 bson_error_t error;
168 bool res;
169
170 BSON_ASSERT (other_host_and_port);
171 command = BCON_NEW ("cloneCollection",
172 BCON_UTF8 ("test.remoteThings"),
173 "from",
174 BCON_UTF8 (other_host_and_port),
175 "query",
176 "{",
177 "x",
178 BCON_INT32 (1),
179 "}");
180 res =
181 mongoc_database_command_simple (database, command, NULL, &reply, &error);
182 if (!res) {
183 fprintf (stderr, "Error with clone: %s\n", error.message);
184 goto cleanup;
185 }
186
187 /* Do something with the reply */
188 print_res (&reply);
189
190 cleanup:
191 bson_destroy (&reply);
192 bson_destroy (command);
193
194 return res;
195 }
196
197
199 common-operations.c.INDENT 0.0
200
201 /*
202 * Copyright 2016 MongoDB, Inc.
203 *
204 * Licensed under the Apache License, Version 2.0 (the "License");
205 * you may not use this file except in compliance with the License.
206 * You may obtain a copy of the License at
207 *
208 * http://www.apache.org/licenses/LICENSE-2.0
209 *
210 * Unless required by applicable law or agreed to in writing, software
211 * distributed under the License is distributed on an "AS IS" BASIS,
212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
213 * See the License for the specific language governing permissions and
214 * limitations under the License.
215 */
216
217
218 #include <mongoc/mongoc.h>
219 #include <stdio.h>
220
221
222 const char *COLLECTION_NAME = "things";
223
224 #include "../doc-common-insert.c"
225 #include "explain.c"
226 #include "copydb.c"
227 #include "clone-collection.c"
228
229
230 int
231 main (int argc, char *argv[])
232 {
233 mongoc_database_t *database = NULL;
234 mongoc_client_t *client = NULL;
235 mongoc_collection_t *collection = NULL;
236 mongoc_uri_t *uri = NULL;
237 bson_error_t error;
238 char *host_and_port;
239 int res = 0;
240 char *other_host_and_port = NULL;
241
242 if (argc < 2 || argc > 3) {
243 fprintf (stderr,
244 "usage: %s MONGOD-1-CONNECTION-STRING "
245 "[MONGOD-2-HOST-NAME:MONGOD-2-PORT]\n",
246 argv[0]);
247 fprintf (stderr,
248 "MONGOD-1-CONNECTION-STRING can be "
249 "of the following forms:\n");
250 fprintf (stderr, "localhost\t\t\t\tlocal machine\n");
251 fprintf (stderr, "localhost:27018\t\t\t\tlocal machine on port 27018\n");
252 fprintf (stderr,
253 "mongodb://user:pass@localhost:27017\t"
254 "local machine on port 27017, and authenticate with username "
255 "user and password pass\n");
256 return EXIT_FAILURE;
257 }
258
259 mongoc_init ();
260
261 if (strncmp (argv[1], "mongodb://", 10) == 0) {
262 host_and_port = bson_strdup (argv[1]);
263 } else {
264 host_and_port = bson_strdup_printf ("mongodb://%s", argv[1]);
265 }
266 other_host_and_port = argc > 2 ? argv[2] : NULL;
267
268 uri = mongoc_uri_new_with_error (host_and_port, &error);
269 if (!uri) {
270 fprintf (stderr,
271 "failed to parse URI: %s\n"
272 "error message: %s\n",
273 host_and_port,
274 error.message);
275 res = EXIT_FAILURE;
276 goto cleanup;
277 }
278
279 client = mongoc_client_new_from_uri (uri);
280 if (!client) {
281 res = EXIT_FAILURE;
282 goto cleanup;
283 }
284
285 mongoc_client_set_error_api (client, 2);
286 database = mongoc_client_get_database (client, "test");
287 collection = mongoc_database_get_collection (database, COLLECTION_NAME);
288
289 printf ("Inserting data\n");
290 if (!insert_data (collection)) {
291 res = EXIT_FAILURE;
292 goto cleanup;
293 }
294
295 printf ("explain\n");
296 if (!explain (collection)) {
297 res = EXIT_FAILURE;
298 goto cleanup;
299 }
300
301 if (other_host_and_port) {
302 printf ("copydb\n");
303 if (!copydb (client, other_host_and_port)) {
304 res = EXIT_FAILURE;
305 goto cleanup;
306 }
307
308 printf ("clone collection\n");
309 if (!clone_collection (database, other_host_and_port)) {
310 res = EXIT_FAILURE;
311 goto cleanup;
312 }
313 }
314
315 cleanup:
316 if (collection) {
317 mongoc_collection_destroy (collection);
318 }
319
320 if (database) {
321 mongoc_database_destroy (database);
322 }
323
324 if (client) {
325 mongoc_client_destroy (client);
326 }
327
328 if (uri) {
329 mongoc_uri_destroy (uri);
330 }
331
332 bson_free (host_and_port);
333 mongoc_cleanup ();
334 return res;
335 }
336
337
338First launch two separate instances of mongod (must be done from separate
339shells):
340
341 $ mongod
342
343 $ mkdir /tmp/db2$ mongod --dbpath /tmp/db2 --port 27018 # second instance
344
345 Now compile and run the example program:
346
347 $ cd examples/common_operations/$ gcc -Wall -o example common-operations.c $(pkg-config --cflags --libs libmongoc-1.0)$ ./example localhost:27017 localhost:27018
348 Inserting data
349 explain
350 {
351 "executionStats" : {
352 "allPlansExecution" : [],
353 "executionStages" : {
354 "advanced" : 19,
355 "direction" : "forward" ,
356 "docsExamined" : 76,
357 "executionTimeMillisEstimate" : 0,
358 "filter" : {
359 "x" : {
360 "$eq" : 1
361 }
362 },
363 "invalidates" : 0,
364 "isEOF" : 1,
365 "nReturned" : 19,
366 "needTime" : 58,
367 "needYield" : 0,
368 "restoreState" : 0,
369 "saveState" : 0,
370 "stage" : "COLLSCAN" ,
371 "works" : 78
372 },
373 "executionSuccess" : true,
374 "executionTimeMillis" : 0,
375 "nReturned" : 19,
376 "totalDocsExamined" : 76,
377 "totalKeysExamined" : 0
378 },
379 "ok" : 1,
380 "queryPlanner" : {
381 "indexFilterSet" : false,
382 "namespace" : "test.things",
383 "parsedQuery" : {
384 "x" : {
385 "$eq" : 1
386 }
387 },
388 "plannerVersion" : 1,
389 "rejectedPlans" : [],
390 "winningPlan" : {
391 "direction" : "forward" ,
392 "filter" : {
393 "x" : {
394 "$eq" : 1
395 }
396 },
397 "stage" : "COLLSCAN"
398 }
399 },
400 "serverInfo" : {
401 "gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25" ,
402 "host" : "MacBook-Pro-57.local",
403 "port" : 27017,
404 "version" : "3.2.6"
405 }
406 }
407 copydb
408 { "ok" : 1 }
409 clone collection
410 { "ok" : 1 }
411
413 MongoDB, Inc
414
416 2017-present, MongoDB, Inc
417
418
419
420
4211.13.1 Jan 24, 2019 MONGOC_COMMON_TASK_EXAMPLES(3)