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