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_modify_opts_ap‐
7 pend()
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
28 mongoc_write_concern_append() to add the write concern to opts. See
29 the example code for mongoc_client_write_command_with_opts().
30
31 • sessionId: First, construct a mongoc_client_session_t with
32 mongoc_client_start_session(). You can begin a transaction with
33 mongoc_client_session_start_transaction(), optionally with a
34 mongoc_transaction_opt_t that overrides the options inherited from
35 collection, and use mongoc_client_session_append() to add the session
36 to opts. See 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
41 • let: A BSON document consisting of any number of parameter names,
42 each followed by definitions of constants in the MQL Aggregate Ex‐
43 pression language.
44
45 • comment: A bson_value_t specifying the comment to attach to this com‐
46 mand. The comment will appear in log messages, profiler output, and
47 currentOp output. Requires MongoDB 4.4 or later.
48
50 Returns true on success. If any arguments are invalid, returns false
51 and logs an error.
52
54 opts.c
55
56 void
57 fam_opts (mongoc_collection_t *collection)
58 {
59 mongoc_find_and_modify_opts_t *opts;
60 bson_t reply;
61 bson_t *update;
62 bson_error_t error;
63 bson_t query = BSON_INITIALIZER;
64 mongoc_write_concern_t *wc;
65 bson_t extra = BSON_INITIALIZER;
66 bool success;
67
68
69 /* Find Zlatan Ibrahimovic, the striker */
70 BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
71 BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
72 BSON_APPEND_UTF8 (&query, "profession", "Football player");
73
74 /* Bump his age */
75 update = BCON_NEW ("$inc", "{", "age", BCON_INT32 (1), "}");
76
77 opts = mongoc_find_and_modify_opts_new ();
78 mongoc_find_and_modify_opts_set_update (opts, update);
79
80 /* Abort if the operation takes too long. */
81 mongoc_find_and_modify_opts_set_max_time_ms (opts, 100);
82
83 /* Set write concern w: 2 */
84 wc = mongoc_write_concern_new ();
85 mongoc_write_concern_set_w (wc, 2);
86 mongoc_write_concern_append (wc, &extra);
87
88 /* Some future findAndModify option the driver doesn't support conveniently
89 */
90 BSON_APPEND_INT32 (&extra, "futureOption", 42);
91 mongoc_find_and_modify_opts_append (opts, &extra);
92
93 success = mongoc_collection_find_and_modify_with_opts (
94 collection, &query, opts, &reply, &error);
95
96 if (success) {
97 char *str;
98
99 str = bson_as_canonical_extended_json (&reply, NULL);
100 printf ("%s\n", str);
101 bson_free (str);
102 } else {
103 fprintf (
104 stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
105 }
106
107 bson_destroy (&reply);
108 bson_destroy (&extra);
109 bson_destroy (update);
110 bson_destroy (&query);
111 mongoc_write_concern_destroy (wc);
112 mongoc_find_and_modify_opts_destroy (opts);
113 }
114
115
117 MongoDB, Inc
118
120 2017-present, MongoDB, Inc
121
122
123
124
1251.23.1 Oct 20, 2M0O2N2GOC_FIND_AND_MODIFY_OPTS_APPEND(3)