1MONGOC_COMMON_TASK_EXAMPLES(3)     libmongoc    MONGOC_COMMON_TASK_EXAMPLES(3)
2
3
4

NAME

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

SETUP

13       First we'll write some code to insert sample data:
14
15       doc-common-insert.c
16
17          /* Don't try to compile this file on its own. It's meant to be #included
18             by example code */
19
20          /* Insert some sample data */
21          bool
22          insert_data (mongoc_collection_t *collection)
23          {
24             mongoc_bulk_operation_t *bulk;
25             enum N { ndocs = 4 };
26             bson_t *docs[ndocs];
27             bson_error_t error;
28             int i = 0;
29             bool ret;
30
31             bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
32
33             docs[0] = BCON_NEW ("x", BCON_DOUBLE (1.0), "tags", "[", "dog", "cat", "]");
34             docs[1] = BCON_NEW ("x", BCON_DOUBLE (2.0), "tags", "[", "cat", "]");
35             docs[2] = BCON_NEW (
36                "x", BCON_DOUBLE (2.0), "tags", "[", "mouse", "cat", "dog", "]");
37             docs[3] = BCON_NEW ("x", BCON_DOUBLE (3.0), "tags", "[", "]");
38
39             for (i = 0; i < ndocs; i++) {
40                mongoc_bulk_operation_insert (bulk, docs[i]);
41                bson_destroy (docs[i]);
42                docs[i] = NULL;
43             }
44
45             ret = mongoc_bulk_operation_execute (bulk, NULL, &error);
46
47             if (!ret) {
48                fprintf (stderr, "Error inserting data: %s\n", error.message);
49             }
50
51             mongoc_bulk_operation_destroy (bulk);
52             return ret;
53          }
54
55          /* A helper which we'll use a lot later on */
56          void
57          print_res (const bson_t *reply)
58          {
59             char *str;
60             BSON_ASSERT (reply);
61             str = bson_as_canonical_extended_json (reply, NULL);
62             printf ("%s\n", str);
63             bson_free (str);
64          }
65
66

EXPLAIN COMMAND

68       This is how to use the explain command in MongoDB 3.2+:
69
70       explain.c
71
72          bool
73          explain (mongoc_collection_t *collection)
74          {
75             bson_t *command;
76             bson_t reply;
77             bson_error_t error;
78             bool res;
79
80             command = BCON_NEW ("explain",
81                                 "{",
82                                 "find",
83                                 BCON_UTF8 (COLLECTION_NAME),
84                                 "filter",
85                                 "{",
86                                 "x",
87                                 BCON_INT32 (1),
88                                 "}",
89                                 "}");
90             res = mongoc_collection_command_simple (
91                collection, command, NULL, &reply, &error);
92             if (!res) {
93                fprintf (stderr, "Error with explain: %s\n", error.message);
94                goto cleanup;
95             }
96
97             /* Do something with the reply */
98             print_res (&reply);
99
100          cleanup:
101             bson_destroy (&reply);
102             bson_destroy (command);
103             return res;
104          }
105
106

RUNNING THE EXAMPLES

108       common-operations.c
109
110          /*
111           * Copyright 2016 MongoDB, Inc.
112           *
113           * Licensed under the Apache License, Version 2.0 (the "License");
114           * you may not use this file except in compliance with the License.
115           * You may obtain a copy of the License at
116           *
117           *   http://www.apache.org/licenses/LICENSE-2.0
118           *
119           * Unless required by applicable law or agreed to in writing, software
120           * distributed under the License is distributed on an "AS IS" BASIS,
121           * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122           * See the License for the specific language governing permissions and
123           * limitations under the License.
124           */
125
126
127          #include <mongoc/mongoc.h>
128          #include <stdio.h>
129
130
131          const char *COLLECTION_NAME = "things";
132
133          #include "../doc-common-insert.c"
134          #include "explain.c"
135
136
137          int
138          main (int argc, char *argv[])
139          {
140             mongoc_database_t *database = NULL;
141             mongoc_client_t *client = NULL;
142             mongoc_collection_t *collection = NULL;
143             mongoc_uri_t *uri = NULL;
144             bson_error_t error;
145             char *host_and_port;
146             int res = 0;
147
148             if (argc < 2 || argc > 3) {
149                fprintf (stderr,
150                         "usage: %s MONGOD-1-CONNECTION-STRING "
151                         "[MONGOD-2-HOST-NAME:MONGOD-2-PORT]\n",
152                         argv[0]);
153                fprintf (stderr,
154                         "MONGOD-1-CONNECTION-STRING can be "
155                         "of the following forms:\n");
156                fprintf (stderr, "localhost\t\t\t\tlocal machine\n");
157                fprintf (stderr, "localhost:27018\t\t\t\tlocal machine on port 27018\n");
158                fprintf (stderr,
159                         "mongodb://user:pass@localhost:27017\t"
160                         "local machine on port 27017, and authenticate with username "
161                         "user and password pass\n");
162                return EXIT_FAILURE;
163             }
164
165             mongoc_init ();
166
167             if (strncmp (argv[1], "mongodb://", 10) == 0) {
168                host_and_port = bson_strdup (argv[1]);
169             } else {
170                host_and_port = bson_strdup_printf ("mongodb://%s", argv[1]);
171             }
172
173             uri = mongoc_uri_new_with_error (host_and_port, &error);
174             if (!uri) {
175                fprintf (stderr,
176                         "failed to parse URI: %s\n"
177                         "error message:       %s\n",
178                         host_and_port,
179                         error.message);
180                res = EXIT_FAILURE;
181                goto cleanup;
182             }
183
184             client = mongoc_client_new_from_uri (uri);
185             if (!client) {
186                res = EXIT_FAILURE;
187                goto cleanup;
188             }
189
190             mongoc_client_set_error_api (client, 2);
191             database = mongoc_client_get_database (client, "test");
192             collection = mongoc_database_get_collection (database, COLLECTION_NAME);
193
194             printf ("Inserting data\n");
195             if (!insert_data (collection)) {
196                res = EXIT_FAILURE;
197                goto cleanup;
198             }
199
200             printf ("explain\n");
201             if (!explain (collection)) {
202                res = EXIT_FAILURE;
203                goto cleanup;
204             }
205
206          cleanup:
207             if (collection) {
208                mongoc_collection_destroy (collection);
209             }
210
211             if (database) {
212                mongoc_database_destroy (database);
213             }
214
215             if (client) {
216                mongoc_client_destroy (client);
217             }
218
219             if (uri) {
220                mongoc_uri_destroy (uri);
221             }
222
223             bson_free (host_and_port);
224             mongoc_cleanup ();
225             return res;
226          }
227
228
229       First  launch two separate instances of mongod (must be done from sepa‐
230       rate shells):
231
232          $ mongod
233
234          $ mkdir /tmp/db2
235          $ mongod --dbpath /tmp/db2 --port 27018 # second instance
236
237       Now compile and run the example program:
238
239          $ cd examples/common_operations/$ gcc -Wall -o example common-operations.c $(pkg-config --cflags --libs libmongoc-1.0)$ ./example localhost:27017 localhost:27018
240          Inserting data
241          explain
242          {
243             "executionStats" : {
244                "allPlansExecution" : [],
245                "executionStages" : {
246                   "advanced" : 19,
247                   "direction" : "forward" ,
248                   "docsExamined" : 76,
249                   "executionTimeMillisEstimate" : 0,
250                   "filter" : {
251                      "x" : {
252                         "$eq" : 1
253                      }
254                   },
255                   "invalidates" : 0,
256                   "isEOF" : 1,
257                   "nReturned" : 19,
258                   "needTime" : 58,
259                   "needYield" : 0,
260                   "restoreState" : 0,
261                   "saveState" : 0,
262                   "stage" : "COLLSCAN" ,
263                   "works" : 78
264                },
265                "executionSuccess" : true,
266                "executionTimeMillis" : 0,
267                "nReturned" : 19,
268                "totalDocsExamined" : 76,
269                "totalKeysExamined" : 0
270             },
271             "ok" : 1,
272             "queryPlanner" : {
273                "indexFilterSet" : false,
274                "namespace" : "test.things",
275                "parsedQuery" : {
276                   "x" : {
277                      "$eq" : 1
278                   }
279                },
280                "plannerVersion" : 1,
281                "rejectedPlans" : [],
282                "winningPlan" : {
283                   "direction" : "forward" ,
284                   "filter" : {
285                      "x" : {
286                         "$eq" : 1
287                      }
288                   },
289                   "stage" : "COLLSCAN"
290                }
291             },
292             "serverInfo" : {
293                "gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25" ,
294                "host" : "MacBook-Pro-57.local",
295                "port" : 27017,
296                "version" : "3.2.6"
297             }
298          }
299

AUTHOR

301       MongoDB, Inc
302
304       2017-present, MongoDB, Inc
305
306
307
308
3091.20.0                           Nov 18, 2021   MONGOC_COMMON_TASK_EXAMPLES(3)
Impressum