1MONGOC_COMMON_TASK_EXAMPLES(3) libmongoc MONGOC_COMMON_TASK_EXAMPLES(3)
2
3
4
5Drivers for some other languages provide helper functions to perform certain
6common tasks. In the C Driver we must explicitly build commands to send to the
7server.
8
10 First we'll write some code to insert sample data:
11
12 doc-common-insert.c
13
14 /* Don't try to compile this file on its own. It's meant to be #included
15 by example code */
16
17 /* Insert some sample data */
18 bool
19 insert_data (mongoc_collection_t *collection)
20 {
21 mongoc_bulk_operation_t *bulk;
22 enum N { ndocs = 4 };
23 bson_t *docs[ndocs];
24 bson_error_t error;
25 int i = 0;
26 bool ret;
27
28 bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
29
30 docs[0] = BCON_NEW ("x", BCON_DOUBLE (1.0), "tags", "[", "dog", "cat", "]");
31 docs[1] = BCON_NEW ("x", BCON_DOUBLE (2.0), "tags", "[", "cat", "]");
32 docs[2] = BCON_NEW (
33 "x", BCON_DOUBLE (2.0), "tags", "[", "mouse", "cat", "dog", "]");
34 docs[3] = BCON_NEW ("x", BCON_DOUBLE (3.0), "tags", "[", "]");
35
36 for (i = 0; i < ndocs; i++) {
37 mongoc_bulk_operation_insert (bulk, docs[i]);
38 bson_destroy (docs[i]);
39 docs[i] = NULL;
40 }
41
42 ret = mongoc_bulk_operation_execute (bulk, NULL, &error);
43
44 if (!ret) {
45 fprintf (stderr, "Error inserting data: %s\n", error.message);
46 }
47
48 mongoc_bulk_operation_destroy (bulk);
49 return ret;
50 }
51
52 /* A helper which we'll use a lot later on */
53 void
54 print_res (const bson_t *reply)
55 {
56 char *str;
57 BSON_ASSERT (reply);
58 str = bson_as_canonical_extended_json (reply, NULL);
59 printf ("%s\n", str);
60 bson_free (str);
61 }
62
63
65 This is how to use the explain command in MongoDB 3.2+:
66
67 explain.c
68
69 bool
70 explain (mongoc_collection_t *collection)
71 {
72 bson_t *command;
73 bson_t reply;
74 bson_error_t error;
75 bool res;
76
77 command = BCON_NEW ("explain",
78 "{",
79 "find",
80 BCON_UTF8 (COLLECTION_NAME),
81 "filter",
82 "{",
83 "x",
84 BCON_INT32 (1),
85 "}",
86 "}");
87 res = mongoc_collection_command_simple (
88 collection, command, NULL, &reply, &error);
89 if (!res) {
90 fprintf (stderr, "Error with explain: %s\n", error.message);
91 goto cleanup;
92 }
93
94 /* Do something with the reply */
95 print_res (&reply);
96
97 cleanup:
98 bson_destroy (&reply);
99 bson_destroy (command);
100 return res;
101 }
102
103
105 common-operations.c
106
107 /*
108 * Copyright 2016 MongoDB, Inc.
109 *
110 * Licensed under the Apache License, Version 2.0 (the "License");
111 * you may not use this file except in compliance with the License.
112 * You may obtain a copy of the License at
113 *
114 * http://www.apache.org/licenses/LICENSE-2.0
115 *
116 * Unless required by applicable law or agreed to in writing, software
117 * distributed under the License is distributed on an "AS IS" BASIS,
118 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
119 * See the License for the specific language governing permissions and
120 * limitations under the License.
121 */
122
123
124 #include <mongoc/mongoc.h>
125 #include <stdio.h>
126
127
128 const char *COLLECTION_NAME = "things";
129
130 #include "../doc-common-insert.c"
131 #include "explain.c"
132
133
134 int
135 main (int argc, char *argv[])
136 {
137 mongoc_database_t *database = NULL;
138 mongoc_client_t *client = NULL;
139 mongoc_collection_t *collection = NULL;
140 mongoc_uri_t *uri = NULL;
141 bson_error_t error;
142 char *host_and_port;
143 int res = 0;
144
145 if (argc < 2 || argc > 3) {
146 fprintf (stderr,
147 "usage: %s MONGOD-1-CONNECTION-STRING "
148 "[MONGOD-2-HOST-NAME:MONGOD-2-PORT]\n",
149 argv[0]);
150 fprintf (stderr,
151 "MONGOD-1-CONNECTION-STRING can be "
152 "of the following forms:\n");
153 fprintf (stderr, "localhost\t\t\t\tlocal machine\n");
154 fprintf (stderr, "localhost:27018\t\t\t\tlocal machine on port 27018\n");
155 fprintf (stderr,
156 "mongodb://user:pass@localhost:27017\t"
157 "local machine on port 27017, and authenticate with username "
158 "user and password pass\n");
159 return EXIT_FAILURE;
160 }
161
162 mongoc_init ();
163
164 if (strncmp (argv[1], "mongodb://", 10) == 0) {
165 host_and_port = bson_strdup (argv[1]);
166 } else {
167 host_and_port = bson_strdup_printf ("mongodb://%s", argv[1]);
168 }
169
170 uri = mongoc_uri_new_with_error (host_and_port, &error);
171 if (!uri) {
172 fprintf (stderr,
173 "failed to parse URI: %s\n"
174 "error message: %s\n",
175 host_and_port,
176 error.message);
177 res = EXIT_FAILURE;
178 goto cleanup;
179 }
180
181 client = mongoc_client_new_from_uri (uri);
182 if (!client) {
183 res = EXIT_FAILURE;
184 goto cleanup;
185 }
186
187 mongoc_client_set_error_api (client, 2);
188 database = mongoc_client_get_database (client, "test");
189 collection = mongoc_database_get_collection (database, COLLECTION_NAME);
190
191 printf ("Inserting data\n");
192 if (!insert_data (collection)) {
193 res = EXIT_FAILURE;
194 goto cleanup;
195 }
196
197 printf ("explain\n");
198 if (!explain (collection)) {
199 res = EXIT_FAILURE;
200 goto cleanup;
201 }
202
203 cleanup:
204 if (collection) {
205 mongoc_collection_destroy (collection);
206 }
207
208 if (database) {
209 mongoc_database_destroy (database);
210 }
211
212 if (client) {
213 mongoc_client_destroy (client);
214 }
215
216 if (uri) {
217 mongoc_uri_destroy (uri);
218 }
219
220 bson_free (host_and_port);
221 mongoc_cleanup ();
222 return res;
223 }
224
225
226 First launch two separate instances of mongod (must be done from sepa‐
227 rate shells):
228
229 $ mongod
230
231 $ mkdir /tmp/db2
232 $ mongod --dbpath /tmp/db2 --port 27018 # second instance
233
234 Now compile and run the example program:
235
236 $ cd examples/common_operations/$ gcc -Wall -o example common-operations.c $(pkg-config --cflags --libs libmongoc-1.0)$ ./example localhost:27017 localhost:27018
237 Inserting data
238 explain
239 {
240 "executionStats" : {
241 "allPlansExecution" : [],
242 "executionStages" : {
243 "advanced" : 19,
244 "direction" : "forward" ,
245 "docsExamined" : 76,
246 "executionTimeMillisEstimate" : 0,
247 "filter" : {
248 "x" : {
249 "$eq" : 1
250 }
251 },
252 "invalidates" : 0,
253 "isEOF" : 1,
254 "nReturned" : 19,
255 "needTime" : 58,
256 "needYield" : 0,
257 "restoreState" : 0,
258 "saveState" : 0,
259 "stage" : "COLLSCAN" ,
260 "works" : 78
261 },
262 "executionSuccess" : true,
263 "executionTimeMillis" : 0,
264 "nReturned" : 19,
265 "totalDocsExamined" : 76,
266 "totalKeysExamined" : 0
267 },
268 "ok" : 1,
269 "queryPlanner" : {
270 "indexFilterSet" : false,
271 "namespace" : "test.things",
272 "parsedQuery" : {
273 "x" : {
274 "$eq" : 1
275 }
276 },
277 "plannerVersion" : 1,
278 "rejectedPlans" : [],
279 "winningPlan" : {
280 "direction" : "forward" ,
281 "filter" : {
282 "x" : {
283 "$eq" : 1
284 }
285 },
286 "stage" : "COLLSCAN"
287 }
288 },
289 "serverInfo" : {
290 "gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25" ,
291 "host" : "MacBook-Pro-57.local",
292 "port" : 27017,
293 "version" : "3.2.6"
294 }
295 }
296
298 MongoDB, Inc
299
301 2017-present, MongoDB, Inc
302
303
304
305
3061.25.1 Nov 08, 2023 MONGOC_COMMON_TASK_EXAMPLES(3)