1MONGOC_FIND_AND_MODIFY_OPTS_T(3)   libmongoc  MONGOC_FIND_AND_MODIFY_OPTS_T(3)
2
3
4
5find_and_modify abstraction
6

SYNOPSIS

8       mongoc_find_and_modify_opts_t  is  a builder interface to construct the
9       findAndModify command.
10
11       It was created to be able to accommodate new arguments to the  findAndā€
12       Modify command.
13
14       As   of  MongoDB  3.2,  the  mongoc_write_concern_t  specified  on  the
15       mongoc_collection_t will be used, if any.
16

EXAMPLE

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

AUTHOR

498       MongoDB, Inc
499
501       2017-present, MongoDB, Inc
502
503
504
505
5061.25.1                           Nov 08, 2023 MONGOC_FIND_AND_MODIFY_OPTS_T(3)
Impressum