1MONGOC_FIND_AND_MODIFY_OPTS_SET_MUoPnDgAoTDEBM(O3CN)GDOrCi_vFeIrND_AND_MODIFY_OPTS_SET_UPDATE(3)
2
3
4
6 mongoc_find_and_modify_opts_set_update - mongoc_find_and_mod‐
7 ify_opts_set_update()
8
10 bool
11 mongoc_find_and_modify_opts_set_update (mongoc_find_and_modify_opts_t *opts,
12 const bson_t *update);
13
15 · opts: A mongoc_find_and_modify_opts_t.
16
17 · update: The update document is the same format as the update document
18 passed to mongoc_collection_update.
19
21 Adds update argument to the builder.
22
23 update does not have to remain valid after calling this function.
24
26 Returns true if it successfully added the option to the builder, other‐
27 wise false.
28
30 update.c.INDENT 0.0
31
32 void
33 fam_update (mongoc_collection_t *collection)
34 {
35 mongoc_find_and_modify_opts_t *opts;
36 bson_t *update;
37 bson_t reply;
38 bson_error_t error;
39 bson_t query = BSON_INITIALIZER;
40 bool success;
41
42
43 /* Find Zlatan Ibrahimovic */
44 BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
45 BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
46
47 /* Make him a book author */
48 update = BCON_NEW ("$set", "{", "author", BCON_BOOL (true), "}");
49
50 opts = mongoc_find_and_modify_opts_new ();
51 /* Note that the document returned is the _previous_ version of the document
52 * To fetch the modified new version, use
53 * mongoc_find_and_modify_opts_set_flags (opts,
54 * MONGOC_FIND_AND_MODIFY_RETURN_NEW);
55 */
56 mongoc_find_and_modify_opts_set_update (opts, update);
57
58 success = mongoc_collection_find_and_modify_with_opts (
59 collection, &query, opts, &reply, &error);
60
61 if (success) {
62 char *str;
63
64 str = bson_as_canonical_extended_json (&reply, NULL);
65 printf ("%s\n", str);
66 bson_free (str);
67 } else {
68 fprintf (
69 stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
70 }
71
72 bson_destroy (&reply);
73 bson_destroy (update);
74 bson_destroy (&query);
75 mongoc_find_and_modify_opts_destroy (opts);
76 }
77
78
79Outputs:
80
81 {
82 "lastErrorObject" : {"updatedExisting" : true, "n" : 1},
83 "value" : {
84 "_id" : {"$oid" : "56562a99d13e6d86239c7b00"},
85 "age" : 35,
86 "firstname" : "Zlatan",
87 "goals" : 342,
88 "lastname" : "Ibrahimovic",
89 "profession" : "Football player",
90 "position" : "striker"
91 },
92 "ok" : 1
93 }
94
96 MongoDB, Inc
97
99 2017-present, MongoDB, Inc
100
101
102
103
1041.15.2 Nov 0M6O,NG2O0C1_9FIND_AND_MODIFY_OPTS_SET_UPDATE(3)