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