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