1MONGOC_FIND_AND_MODIFY_OPTS_SET_FIELlDiSbM(mO3oN)nGgOoCc_FIND_AND_MODIFY_OPTS_SET_FIELDS(3)
2
3
4
6 bool
7 mongoc_find_and_modify_opts_set_fields (mongoc_find_and_modify_opts_t *opts,
8 const bson_t *fields);
9
11 • opts: A mongoc_find_and_modify_opts_t.
12
13 • fields: A subset of fields to return. Choose which fields to include
14 by appending {fieldname: 1} for each fieldname, or excluding it with
15 {fieldname: 0}.
16
18 Adds fields argument to the builder.
19
20 fields does not have to remain valid after calling this function.
21
23 Returns true if it successfully added the option to the builder, other‐
24 wise false.
25
27 fields.c
28
29 void
30 fam_fields (mongoc_collection_t *collection)
31 {
32 mongoc_find_and_modify_opts_t *opts;
33 bson_t fields = BSON_INITIALIZER;
34 bson_t *update;
35 bson_t reply;
36 bson_error_t error;
37 bson_t query = BSON_INITIALIZER;
38 bool success;
39
40
41 /* Find Zlatan Ibrahimovic */
42 BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
43 BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
44
45 /* Return his goal tally */
46 BSON_APPEND_INT32 (&fields, "goals", 1);
47
48 /* Bump his goal tally */
49 update = BCON_NEW ("$inc", "{", "goals", BCON_INT32 (1), "}");
50
51 opts = mongoc_find_and_modify_opts_new ();
52 mongoc_find_and_modify_opts_set_update (opts, update);
53 mongoc_find_and_modify_opts_set_fields (opts, &fields);
54 /* Return the new tally */
55 mongoc_find_and_modify_opts_set_flags (opts,
56 MONGOC_FIND_AND_MODIFY_RETURN_NEW);
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 (&fields);
75 bson_destroy (&query);
76 mongoc_find_and_modify_opts_destroy (opts);
77 }
78
79
80 Outputs:
81
82 {
83 "lastErrorObject"
84 : {"updatedExisting" : true, "n" : 1},
85 "value"
86 : {"_id" : {"$oid" : "56562a99d13e6d86239c7b00"}, "goals" : 343},
87 "ok" : 1
88 }
89
91 MongoDB, Inc
92
94 2017-present, MongoDB, Inc
95
96
97
98
991.25.1 Nov 0M8O,NG2O0C2_3FIND_AND_MODIFY_OPTS_SET_FIELDS(3)