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