1MONGOC_FIND_AND_MODIFY_OPTS_APPEND(3l)ibmongMoOcNGOC_FIND_AND_MODIFY_OPTS_APPEND(3)
2
3
4
6 mongoc_find_and_modify_opts_append - mongoc_find_and_mod‐
7 ify_opts_append()
8
10 bool
11 mongoc_find_and_modify_opts_append (mongoc_find_and_modify_opts_t *opts,
12 const bson_t *extra);
13
15 · opts: A mongoc_find_and_modify_opts_t.
16
17 · extra: A bson_t with fields and values to append directly to the fin‐
18 dAndModify command sent to the server.
19
21 Adds arbitrary options to a findAndModify command.
22
23 extra does not have to remain valid after calling this function.
24
25 extra may be NULL or a BSON document with additional command options:
26
27 · writeConcern: Construct a mongoc_write_concern_t and use mon‐
28 goc_write_concern_append to add the write concern to opts. See the
29 example code for mongoc_client_write_command_with_opts.
30
31 · sessionId: First, construct a mongoc_client_session_t with mon‐
32 goc_client_start_session. You can begin a transaction with mon‐
33 goc_client_session_start_transaction, optionally with a mongoc_trans‐
34 action_opt_t that overrides the options inherited from collection,
35 and use mongoc_client_session_append to add the session to opts. See
36 the example code for mongoc_client_session_t.
37
38 · hint: A document or string that specifies the index to use to support
39 the query predicate.
40
42 Returns true on success. If any arguments are invalid, returns false
43 and logs an error.
44
46 opts.c
47
48 void
49 fam_opts (mongoc_collection_t *collection)
50 {
51 mongoc_find_and_modify_opts_t *opts;
52 bson_t reply;
53 bson_t *update;
54 bson_error_t error;
55 bson_t query = BSON_INITIALIZER;
56 mongoc_write_concern_t *wc;
57 bson_t extra = BSON_INITIALIZER;
58 bool success;
59
60
61 /* Find Zlatan Ibrahimovic, the striker */
62 BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
63 BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
64 BSON_APPEND_UTF8 (&query, "profession", "Football player");
65
66 /* Bump his age */
67 update = BCON_NEW ("$inc", "{", "age", BCON_INT32 (1), "}");
68
69 opts = mongoc_find_and_modify_opts_new ();
70 mongoc_find_and_modify_opts_set_update (opts, update);
71
72 /* Abort if the operation takes too long. */
73 mongoc_find_and_modify_opts_set_max_time_ms (opts, 100);
74
75 /* Set write concern w: 2 */
76 wc = mongoc_write_concern_new ();
77 mongoc_write_concern_set_w (wc, 2);
78 mongoc_write_concern_append (wc, &extra);
79
80 /* Some future findAndModify option the driver doesn't support conveniently
81 */
82 BSON_APPEND_INT32 (&extra, "futureOption", 42);
83 mongoc_find_and_modify_opts_append (opts, &extra);
84
85 success = mongoc_collection_find_and_modify_with_opts (
86 collection, &query, opts, &reply, &error);
87
88 if (success) {
89 char *str;
90
91 str = bson_as_canonical_extended_json (&reply, NULL);
92 printf ("%s\n", str);
93 bson_free (str);
94 } else {
95 fprintf (
96 stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
97 }
98
99 bson_destroy (&reply);
100 bson_destroy (&extra);
101 bson_destroy (update);
102 bson_destroy (&query);
103 mongoc_write_concern_destroy (wc);
104 mongoc_find_and_modify_opts_destroy (opts);
105 }
106
107
109 MongoDB, Inc
110
112 2017-present, MongoDB, Inc
113
114
115
116
1171.17.4 Feb 04, 2M0O2N1GOC_FIND_AND_MODIFY_OPTS_APPEND(3)