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