1MONGOC_COLLECTION_FIND_AND_MODIFY(3)libmongoMcONGOC_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 As of MongoDB 3.2, the mongoc_write_concern_t specified on the
50 mongoc_collection_t will be used, if any.
51
52 reply is always initialized, and must be freed with bson_destroy().
53
54 On success, the output reply contains the full server reply to the fin‐
55 dAndModify command. See the MongoDB Manual page for findAndModify for
56 the expected server reply.
57
59 Errors are propagated via the error parameter.
60
62 If given invalid arguments or a server/network error occurs, returns
63 false and sets error. Otherwise, succeeds and returns true. A write
64 concern timeout or write concern error is considered a failure.
65
66 SEE ALSO:
67 mongoc_collection_find_and_modify_with_opts().
68
69
71 find-and-modify.c
72
73 #include <mongoc/mongoc.h>
74 #include <stdio.h>
75
76
77 int
78 main (void)
79 {
80 mongoc_collection_t *collection;
81 mongoc_client_t *client;
82 const char *uri_string =
83 "mongodb://127.0.0.1:27017/?appname=find-and-modify-example";
84 mongoc_uri_t *uri;
85 bson_error_t error;
86 bson_t *query;
87 bson_t *update;
88 bson_t reply;
89 char *str;
90
91 mongoc_init ();
92
93 uri = mongoc_uri_new_with_error (uri_string, &error);
94 if (!uri) {
95 fprintf (stderr,
96 "failed to parse URI: %s\n"
97 "error message: %s\n",
98 uri_string,
99 error.message);
100 return EXIT_FAILURE;
101 }
102
103 client = mongoc_client_new_from_uri (uri);
104 if (!client) {
105 return EXIT_FAILURE;
106 }
107
108 mongoc_client_set_error_api (client, 2);
109 collection = mongoc_client_get_collection (client, "test", "test");
110
111 /*
112 * Build our query, {"cmpxchg": 1}
113 */
114 query = BCON_NEW ("cmpxchg", BCON_INT32 (1));
115
116 /*
117 * Build our update. {"$set": {"cmpxchg": 2}}
118 */
119 update = BCON_NEW ("$set", "{", "cmpxchg", BCON_INT32 (2), "}");
120
121 /*
122 * Submit the findAndModify.
123 */
124 if (!mongoc_collection_find_and_modify (collection,
125 query,
126 NULL,
127 update,
128 NULL,
129 false,
130 false,
131 true,
132 &reply,
133 &error)) {
134 fprintf (stderr, "find_and_modify() failure: %s\n", error.message);
135 return EXIT_FAILURE;
136 }
137
138 /*
139 * Print the result as JSON.
140 */
141 str = bson_as_canonical_extended_json (&reply, NULL);
142 printf ("%s\n", str);
143 bson_free (str);
144
145 /*
146 * Cleanup.
147 */
148 bson_destroy (query);
149 bson_destroy (update);
150 bson_destroy (&reply);
151 mongoc_collection_destroy (collection);
152 mongoc_uri_destroy (uri);
153 mongoc_client_destroy (client);
154
155 mongoc_cleanup ();
156
157 return EXIT_SUCCESS;
158 }
159
160
162 MongoDB, Inc
163
165 2017-present, MongoDB, Inc
166
167
168
169
1701.24.3 Aug 17, 20M2O3NGOC_COLLECTION_FIND_AND_MODIFY(3)