1MONGOC_COLLECTION_FIND_AND_MODIFMYo(n3g)oDB C DrMiOvNeGrOC_COLLECTION_FIND_AND_MODIFY(3)
2
3
4
6 mongoc_collection_find_and_modify - mongoc_collection_find_and_modify()
7
9 bool
10 mongoc_collection_find_and_modify (mongoc_collection_t *collection,
11 const bson_t *query,
12 const bson_t *sort,
13 const bson_t *update,
14 const bson_t *fields,
15 bool _remove,
16 bool upsert,
17 bool _new,
18 bson_t *reply,
19 bson_error_t *error);
20
22 · collection: A mongoc_collection_t.
23
24 · query: A bson_t containing the query to locate target document(s).
25
26 · sort: A bson_t containing the sort order for query.
27
28 · update: A bson_t containing an update spec.
29
30 · fields: An optional bson_t containing the fields to return or NULL.
31
32 · _remove: If the matching documents should be removed.
33
34 · upsert: If an upsert should be performed.
35
36 · _new: If the new version of the document should be returned.
37
38 · reply: Optional pointer to an uninitialized bson_t that will be ini‐
39 tialized with the result.
40
41 · error: An optional location for a bson_error_t or NULL.
42
44 Update and return an object.
45
46 This is a thin wrapper around the findAndModify command. Either update
47 or _remove arguments are required.
48
49 See also: mongoc_collection_find_and_modify_with_opts.
50
51 As of MongoDB 3.2, the mongoc_write_concern_t specified on the mon‐
52 goc_collection_t will be used, if any.
53
54 reply is always initialized, and must be freed with bson_destroy().
55
57 Errors are propagated via the error parameter.
58
60 Returns either the document before or after modification based on the
61 _new parameter.
62
63 A write concern timeout or write concern error is considered a failure.
64
66 find-and-modify.c.INDENT 0.0
67
68 #include <bson/bcon.h>
69 #include <mongoc/mongoc.h>
70 #include <stdio.h>
71
72
73 int
74 main (int argc, char *argv[])
75 {
76 mongoc_collection_t *collection;
77 mongoc_client_t *client;
78 const char *uri_string =
79 "mongodb://127.0.0.1:27017/?appname=find-and-modify-example";
80 mongoc_uri_t *uri;
81 bson_error_t error;
82 bson_t *query;
83 bson_t *update;
84 bson_t reply;
85 char *str;
86
87 mongoc_init ();
88
89 uri = mongoc_uri_new_with_error (uri_string, &error);
90 if (!uri) {
91 fprintf (stderr,
92 "failed to parse URI: %s\n"
93 "error message: %s\n",
94 uri_string,
95 error.message);
96 return EXIT_FAILURE;
97 }
98
99 client = mongoc_client_new_from_uri (uri);
100 if (!client) {
101 return EXIT_FAILURE;
102 }
103
104 mongoc_client_set_error_api (client, 2);
105 collection = mongoc_client_get_collection (client, "test", "test");
106
107 /*
108 * Build our query, {"cmpxchg": 1}
109 */
110 query = BCON_NEW ("cmpxchg", BCON_INT32 (1));
111
112 /*
113 * Build our update. {"$set": {"cmpxchg": 2}}
114 */
115 update = BCON_NEW ("$set", "{", "cmpxchg", BCON_INT32 (2), "}");
116
117 /*
118 * Submit the findAndModify.
119 */
120 if (!mongoc_collection_find_and_modify (collection,
121 query,
122 NULL,
123 update,
124 NULL,
125 false,
126 false,
127 true,
128 &reply,
129 &error)) {
130 fprintf (stderr, "find_and_modify() failure: %s\n", error.message);
131 return EXIT_FAILURE;
132 }
133
134 /*
135 * Print the result as JSON.
136 */
137 str = bson_as_canonical_extended_json (&reply, NULL);
138 printf ("%s\n", str);
139 bson_free (str);
140
141 /*
142 * Cleanup.
143 */
144 bson_destroy (query);
145 bson_destroy (update);
146 bson_destroy (&reply);
147 mongoc_collection_destroy (collection);
148 mongoc_uri_destroy (uri);
149 mongoc_client_destroy (client);
150
151 mongoc_cleanup ();
152
153 return EXIT_SUCCESS;
154 }
155
156
158 MongoDB, Inc
159
161 2017-present, MongoDB, Inc
162
163
164
165
1661.13.1 Jan 24, 20M1O9NGOC_COLLECTION_FIND_AND_MODIFY(3)