1MONGOC_FIND_AND_MODIFY_OPTS_SET_FIELlDiSbM(mO3oN)nGgOoCc_FIND_AND_MODIFY_OPTS_SET_FIELDS(3)
2
3
4
6 mongoc_find_and_modify_opts_set_fields - mongoc_find_and_mod‐
7 ify_opts_set_fields()
8
10 bool
11 mongoc_find_and_modify_opts_set_fields (mongoc_find_and_modify_opts_t *opts,
12 const bson_t *fields);
13
15 · opts: A mongoc_find_and_modify_opts_t.
16
17 · fields: A subset of fields to return. Choose which fields to include
18 by appending {fieldname: 1} for each fieldname, or excluding it with
19 {fieldname: 0}.
20
22 Adds fields argument to the builder.
23
24 fields does not have to remain valid after calling this function.
25
27 Returns true if it successfully added the option to the builder, other‐
28 wise false.
29
31 fields.c.INDENT 0.0
32
33 void
34 fam_fields (mongoc_collection_t *collection)
35 {
36 mongoc_find_and_modify_opts_t *opts;
37 bson_t fields = BSON_INITIALIZER;
38 bson_t *update;
39 bson_t reply;
40 bson_error_t error;
41 bson_t query = BSON_INITIALIZER;
42 bool success;
43
44
45 /* Find Zlatan Ibrahimovic */
46 BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
47 BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
48
49 /* Return his goal tally */
50 BSON_APPEND_INT32 (&fields, "goals", 1);
51
52 /* Bump his goal tally */
53 update = BCON_NEW ("$inc", "{", "goals", BCON_INT32 (1), "}");
54
55 opts = mongoc_find_and_modify_opts_new ();
56 mongoc_find_and_modify_opts_set_update (opts, update);
57 mongoc_find_and_modify_opts_set_fields (opts, &fields);
58 /* Return the new tally */
59 mongoc_find_and_modify_opts_set_flags (opts,
60 MONGOC_FIND_AND_MODIFY_RETURN_NEW);
61
62 success = mongoc_collection_find_and_modify_with_opts (
63 collection, &query, opts, &reply, &error);
64
65 if (success) {
66 char *str;
67
68 str = bson_as_canonical_extended_json (&reply, NULL);
69 printf ("%s\n", str);
70 bson_free (str);
71 } else {
72 fprintf (
73 stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
74 }
75
76 bson_destroy (&reply);
77 bson_destroy (update);
78 bson_destroy (&fields);
79 bson_destroy (&query);
80 mongoc_find_and_modify_opts_destroy (opts);
81 }
82
83
84Outputs:
85
86 {
87 "lastErrorObject"
88 : {"updatedExisting" : true, "n" : 1},
89 "value"
90 : {"_id" : {"$oid" : "56562a99d13e6d86239c7b00"}, "goals" : 343},
91 "ok" : 1
92 }
93
95 MongoDB, Inc
96
98 2017-present, MongoDB, Inc
99
100
101
102
1031.16.2 Feb 2M5O,NG2O0C2_0FIND_AND_MODIFY_OPTS_SET_FIELDS(3)