1MONGOC_FIND_AND_MODIFY_OPTS_T(3)MongoDB C DriverMONGOC_FIND_AND_MODIFY_OPTS_T(3)
2
3
4

NAME

6       mongoc_find_and_modify_opts_t - mongoc_find_and_modify_opts_t
7
8       find_and_modify abstraction
9

SYNOPSIS

11       mongoc_find_and_modify_opts_t  is  a  builder  interface to construct a
12       find_and_modify command.
13
14       It was created to be able to accommodate new arguments to  the  MongoDB
15       find_and_modify command.
16
17       As  of  MongoDB  3.2,  the mongoc_write_concern_t specified on the mon‐
18       goc_collection_t will be used, if any.
19

EXAMPLE

21       flags.c.INDENT 0.0
22
23          void
24          fam_flags (mongoc_collection_t *collection)
25          {
26             mongoc_find_and_modify_opts_t *opts;
27             bson_t reply;
28             bson_error_t error;
29             bson_t query = BSON_INITIALIZER;
30             bson_t *update;
31             bool success;
32
33
34             /* Find Zlatan Ibrahimovic, the striker */
35             BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
36             BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
37             BSON_APPEND_UTF8 (&query, "profession", "Football player");
38             BSON_APPEND_INT32 (&query, "age", 34);
39             BSON_APPEND_INT32 (
40                &query, "goals", (16 + 35 + 23 + 57 + 16 + 14 + 28 + 84) + (1 + 6 + 62));
41
42             /* Add his football position */
43             update = BCON_NEW ("$set", "{", "position", BCON_UTF8 ("striker"), "}");
44
45             opts = mongoc_find_and_modify_opts_new ();
46
47             mongoc_find_and_modify_opts_set_update (opts, update);
48
49             /* Create the document if it didn't exist, and return the updated document */
50             mongoc_find_and_modify_opts_set_flags (
51                opts, MONGOC_FIND_AND_MODIFY_UPSERT | MONGOC_FIND_AND_MODIFY_RETURN_NEW);
52
53             success = mongoc_collection_find_and_modify_with_opts (
54                collection, &query, opts, &reply, &error);
55
56             if (success) {
57                char *str;
58
59                str = bson_as_canonical_extended_json (&reply, NULL);
60                printf ("%s\n", str);
61                bson_free (str);
62             } else {
63                fprintf (
64                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
65             }
66
67             bson_destroy (&reply);
68             bson_destroy (update);
69             bson_destroy (&query);
70             mongoc_find_and_modify_opts_destroy (opts);
71          }
72
73bypass.c.INDENT 0.0
74
75          void
76          fam_bypass (mongoc_collection_t *collection)
77          {
78             mongoc_find_and_modify_opts_t *opts;
79             bson_t reply;
80             bson_t *update;
81             bson_error_t error;
82             bson_t query = BSON_INITIALIZER;
83             bool success;
84
85
86             /* Find Zlatan Ibrahimovic, the striker */
87             BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
88             BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
89             BSON_APPEND_UTF8 (&query, "profession", "Football player");
90
91             /* Bump his age */
92             update = BCON_NEW ("$inc", "{", "age", BCON_INT32 (1), "}");
93
94             opts = mongoc_find_and_modify_opts_new ();
95             mongoc_find_and_modify_opts_set_update (opts, update);
96             /* He can still play, even though he is pretty old. */
97             mongoc_find_and_modify_opts_set_bypass_document_validation (opts, true);
98
99             success = mongoc_collection_find_and_modify_with_opts (
100                collection, &query, opts, &reply, &error);
101
102             if (success) {
103                char *str;
104
105                str = bson_as_canonical_extended_json (&reply, NULL);
106                printf ("%s\n", str);
107                bson_free (str);
108             } else {
109                fprintf (
110                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
111             }
112
113             bson_destroy (&reply);
114             bson_destroy (update);
115             bson_destroy (&query);
116             mongoc_find_and_modify_opts_destroy (opts);
117          }
118
119update.c.INDENT 0.0
120
121          void
122          fam_update (mongoc_collection_t *collection)
123          {
124             mongoc_find_and_modify_opts_t *opts;
125             bson_t *update;
126             bson_t reply;
127             bson_error_t error;
128             bson_t query = BSON_INITIALIZER;
129             bool success;
130
131
132             /* Find Zlatan Ibrahimovic */
133             BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
134             BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
135
136             /* Make him a book author */
137             update = BCON_NEW ("$set", "{", "author", BCON_BOOL (true), "}");
138
139             opts = mongoc_find_and_modify_opts_new ();
140             /* Note that the document returned is the _previous_ version of the document
141              * To fetch the modified new version, use
142              * mongoc_find_and_modify_opts_set_flags (opts,
143              * MONGOC_FIND_AND_MODIFY_RETURN_NEW);
144              */
145             mongoc_find_and_modify_opts_set_update (opts, update);
146
147             success = mongoc_collection_find_and_modify_with_opts (
148                collection, &query, opts, &reply, &error);
149
150             if (success) {
151                char *str;
152
153                str = bson_as_canonical_extended_json (&reply, NULL);
154                printf ("%s\n", str);
155                bson_free (str);
156             } else {
157                fprintf (
158                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
159             }
160
161             bson_destroy (&reply);
162             bson_destroy (update);
163             bson_destroy (&query);
164             mongoc_find_and_modify_opts_destroy (opts);
165          }
166
167fields.c.INDENT 0.0
168
169          void
170          fam_fields (mongoc_collection_t *collection)
171          {
172             mongoc_find_and_modify_opts_t *opts;
173             bson_t fields = BSON_INITIALIZER;
174             bson_t *update;
175             bson_t reply;
176             bson_error_t error;
177             bson_t query = BSON_INITIALIZER;
178             bool success;
179
180
181             /* Find Zlatan Ibrahimovic */
182             BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
183             BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
184
185             /* Return his goal tally */
186             BSON_APPEND_INT32 (&fields, "goals", 1);
187
188             /* Bump his goal tally */
189             update = BCON_NEW ("$inc", "{", "goals", BCON_INT32 (1), "}");
190
191             opts = mongoc_find_and_modify_opts_new ();
192             mongoc_find_and_modify_opts_set_update (opts, update);
193             mongoc_find_and_modify_opts_set_fields (opts, &fields);
194             /* Return the new tally */
195             mongoc_find_and_modify_opts_set_flags (opts,
196                                                    MONGOC_FIND_AND_MODIFY_RETURN_NEW);
197
198             success = mongoc_collection_find_and_modify_with_opts (
199                collection, &query, opts, &reply, &error);
200
201             if (success) {
202                char *str;
203
204                str = bson_as_canonical_extended_json (&reply, NULL);
205                printf ("%s\n", str);
206                bson_free (str);
207             } else {
208                fprintf (
209                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
210             }
211
212             bson_destroy (&reply);
213             bson_destroy (update);
214             bson_destroy (&fields);
215             bson_destroy (&query);
216             mongoc_find_and_modify_opts_destroy (opts);
217          }
218
219sort.c.INDENT 0.0
220
221          void
222          fam_sort (mongoc_collection_t *collection)
223          {
224             mongoc_find_and_modify_opts_t *opts;
225             bson_t *update;
226             bson_t sort = BSON_INITIALIZER;
227             bson_t reply;
228             bson_error_t error;
229             bson_t query = BSON_INITIALIZER;
230             bool success;
231
232
233             /* Find all users with the lastname Ibrahimovic */
234             BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
235
236             /* Sort by age (descending) */
237             BSON_APPEND_INT32 (&sort, "age", -1);
238
239             /* Bump his goal tally */
240             update = BCON_NEW ("$set", "{", "oldest", BCON_BOOL (true), "}");
241
242             opts = mongoc_find_and_modify_opts_new ();
243             mongoc_find_and_modify_opts_set_update (opts, update);
244             mongoc_find_and_modify_opts_set_sort (opts, &sort);
245
246             success = mongoc_collection_find_and_modify_with_opts (
247                collection, &query, opts, &reply, &error);
248
249             if (success) {
250                char *str;
251
252                str = bson_as_canonical_extended_json (&reply, NULL);
253                printf ("%s\n", str);
254                bson_free (str);
255             } else {
256                fprintf (
257                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
258             }
259
260             bson_destroy (&reply);
261             bson_destroy (update);
262             bson_destroy (&sort);
263             bson_destroy (&query);
264             mongoc_find_and_modify_opts_destroy (opts);
265          }
266
267opts.c.INDENT 0.0
268
269          void
270          fam_opts (mongoc_collection_t *collection)
271          {
272             mongoc_find_and_modify_opts_t *opts;
273             bson_t reply;
274             bson_t *update;
275             bson_error_t error;
276             bson_t query = BSON_INITIALIZER;
277             mongoc_write_concern_t *wc;
278             bson_t extra = BSON_INITIALIZER;
279             bool success;
280
281
282             /* Find Zlatan Ibrahimovic, the striker */
283             BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
284             BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
285             BSON_APPEND_UTF8 (&query, "profession", "Football player");
286
287             /* Bump his age */
288             update = BCON_NEW ("$inc", "{", "age", BCON_INT32 (1), "}");
289
290             opts = mongoc_find_and_modify_opts_new ();
291             mongoc_find_and_modify_opts_set_update (opts, update);
292
293             /* Abort if the operation takes too long. */
294             mongoc_find_and_modify_opts_set_max_time_ms (opts, 100);
295
296             /* Set write concern w: 2 */
297             wc = mongoc_write_concern_new ();
298             mongoc_write_concern_set_w (wc, 2);
299             mongoc_write_concern_append (wc, &extra);
300
301             /* Some future findAndModify option the driver doesn't support conveniently
302              */
303             BSON_APPEND_INT32 (&extra, "futureOption", 42);
304             mongoc_find_and_modify_opts_append (opts, &extra);
305
306             success = mongoc_collection_find_and_modify_with_opts (
307                collection, &query, opts, &reply, &error);
308
309             if (success) {
310                char *str;
311
312                str = bson_as_canonical_extended_json (&reply, NULL);
313                printf ("%s\n", str);
314                bson_free (str);
315             } else {
316                fprintf (
317                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
318             }
319
320             bson_destroy (&reply);
321             bson_destroy (&extra);
322             bson_destroy (update);
323             bson_destroy (&query);
324             mongoc_write_concern_destroy (wc);
325             mongoc_find_and_modify_opts_destroy (opts);
326          }
327
328fam.c.INDENT 0.0
329
330          int
331          main (void)
332          {
333             mongoc_collection_t *collection;
334             mongoc_database_t *database;
335             mongoc_client_t *client;
336             const char *uri_string =
337                "mongodb://localhost:27017/admin?appname=find-and-modify-opts-example";
338             mongoc_uri_t *uri;
339             bson_error_t error;
340             bson_t *options;
341
342             mongoc_init ();
343
344             uri = mongoc_uri_new_with_error (uri_string, &error);
345             if (!uri) {
346                fprintf (stderr,
347                         "failed to parse URI: %s\n"
348                         "error message:       %s\n",
349                         uri_string,
350                         error.message);
351                return EXIT_FAILURE;
352             }
353
354             client = mongoc_client_new_from_uri (uri);
355             if (!client) {
356                return EXIT_FAILURE;
357             }
358
359             mongoc_client_set_error_api (client, 2);
360             database = mongoc_client_get_database (client, "databaseName");
361
362             options = BCON_NEW ("validator",
363                                 "{",
364                                 "age",
365                                 "{",
366                                 "$lte",
367                                 BCON_INT32 (34),
368                                 "}",
369                                 "}",
370                                 "validationAction",
371                                 BCON_UTF8 ("error"),
372                                 "validationLevel",
373                                 BCON_UTF8 ("moderate"));
374
375             collection = mongoc_database_create_collection (
376                database, "collectionName", options, &error);
377             if (!collection) {
378                fprintf (
379                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
380                return EXIT_FAILURE;
381             }
382
383             fam_flags (collection);
384             fam_bypass (collection);
385             fam_update (collection);
386             fam_fields (collection);
387             fam_opts (collection);
388             fam_sort (collection);
389
390             mongoc_collection_drop (collection, NULL);
391             bson_destroy (options);
392             mongoc_uri_destroy (uri);
393             mongoc_database_destroy (database);
394             mongoc_collection_destroy (collection);
395             mongoc_client_destroy (client);
396
397             mongoc_cleanup ();
398             return EXIT_SUCCESS;
399          }
400
401
402Outputs:
403
404          {
405              "lastErrorObject": {
406                  "updatedExisting": false,
407                  "n": 1,
408                  "upserted": {
409                      "$oid": "56562a99d13e6d86239c7b00"
410                  }
411              },
412              "value": {
413                  "_id": {
414                      "$oid": "56562a99d13e6d86239c7b00"
415                  },
416                  "age": 34,
417                  "firstname": "Zlatan",
418                  "goals": 342,
419                  "lastname": "Ibrahimovic",
420                  "profession": "Football player",
421                  "position": "striker"
422              },
423              "ok": 1
424          }
425          {
426              "lastErrorObject": {
427                  "updatedExisting": true,
428                  "n": 1
429              },
430              "value": {
431                  "_id": {
432                      "$oid": "56562a99d13e6d86239c7b00"
433                  },
434                  "age": 34,
435                  "firstname": "Zlatan",
436                  "goals": 342,
437                  "lastname": "Ibrahimovic",
438                  "profession": "Football player",
439                  "position": "striker"
440              },
441              "ok": 1
442          }
443          {
444              "lastErrorObject": {
445                  "updatedExisting": true,
446                  "n": 1
447              },
448              "value": {
449                  "_id": {
450                      "$oid": "56562a99d13e6d86239c7b00"
451                  },
452                  "age": 35,
453                  "firstname": "Zlatan",
454                  "goals": 342,
455                  "lastname": "Ibrahimovic",
456                  "profession": "Football player",
457                  "position": "striker"
458              },
459              "ok": 1
460          }
461          {
462              "lastErrorObject": {
463                  "updatedExisting": true,
464                  "n": 1
465              },
466              "value": {
467                  "_id": {
468                      "$oid": "56562a99d13e6d86239c7b00"
469                  },
470                  "goals": 343
471              },
472              "ok": 1
473          }
474          {
475              "lastErrorObject": {
476                  "updatedExisting": true,
477                  "n": 1
478              },
479              "value": {
480                  "_id": {
481                      "$oid": "56562a99d13e6d86239c7b00"
482                  },
483                  "age": 35,
484                  "firstname": "Zlatan",
485                  "goals": 343,
486                  "lastname": "Ibrahimovic",
487                  "profession": "Football player",
488                  "position": "striker",
489                  "author": true
490              },
491              "ok": 1
492          }
493

AUTHOR

495       MongoDB, Inc
496
498       2017-present, MongoDB, Inc
499
500
501
502
5031.15.2                           Nov 06, 2019 MONGOC_FIND_AND_MODIFY_OPTS_T(3)
Impressum