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