1 #include <bcon.h>
2 #include <mongoc.h>
3 #include <stdio.h>
4
5
6 int
main(int argc,char * argv[])7 main (int argc, char *argv[])
8 {
9 mongoc_collection_t *collection;
10 mongoc_client_t *client;
11 bson_error_t error;
12 bson_t *query;
13 bson_t *update;
14 bson_t reply;
15 char *str;
16
17 mongoc_init ();
18
19 client = mongoc_client_new (
20 "mongodb://127.0.0.1:27017/?appname=find-and-modify-example");
21 mongoc_client_set_error_api (client, 2);
22 collection = mongoc_client_get_collection (client, "test", "test");
23
24 /*
25 * Build our query, {"cmpxchg": 1}
26 */
27 query = BCON_NEW ("cmpxchg", BCON_INT32 (1));
28
29 /*
30 * Build our update. {"$set": {"cmpxchg": 2}}
31 */
32 update = BCON_NEW ("$set", "{", "cmpxchg", BCON_INT32 (2), "}");
33
34 /*
35 * Submit the findAndModify.
36 */
37 if (!mongoc_collection_find_and_modify (collection,
38 query,
39 NULL,
40 update,
41 NULL,
42 false,
43 false,
44 true,
45 &reply,
46 &error)) {
47 fprintf (stderr, "find_and_modify() failure: %s\n", error.message);
48 return 1;
49 }
50
51 /*
52 * Print the result as JSON.
53 */
54 str = bson_as_canonical_extended_json (&reply, NULL);
55 printf ("%s\n", str);
56 bson_free (str);
57
58 /*
59 * Cleanup.
60 */
61 bson_destroy (query);
62 bson_destroy (update);
63 bson_destroy (&reply);
64 mongoc_collection_destroy (collection);
65 mongoc_client_destroy (client);
66
67 mongoc_cleanup ();
68
69 return 0;
70 }
71