#include <bcon.h>
|
#include <mongoc.h>
|
|
void fam_collation(mongoc_collection_t *collection)
|
{
|
mongoc_find_and_modify_opts_t *opts;
|
bson_t *update;
|
bson_t *collation;
|
bson_t reply;
|
bson_error_t error;
|
bson_t query = BSON_INITIALIZER;
|
bool success;
|
|
/* Find all users with the lastname Ibrahimovic */
|
BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
|
|
/* Bump his goal tally */
|
update = BCON_NEW ("$set", "{",
|
"oldest", BCON_BOOL (true),
|
"}");
|
|
collation = BCON_NEW ("collation", "{", "locale", "en_US", "strength", BCON_INT32 (2), "}");
|
|
opts = mongoc_find_and_modify_opts_new ();
|
mongoc_find_and_modify_opts_set_update (opts, update);
|
|
mongoc_find_and_modify_opts_append (opts, collation);
|
|
success = mongoc_collection_find_and_modify_with_opts (collection, &query, opts, &reply, &error);
|
|
if (success) {
|
char *str;
|
|
str = bson_as_json (&reply, NULL);
|
printf ("%s\n", str);
|
bson_free (str);
|
} else {
|
fprintf(stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
|
}
|
|
bson_destroy (&reply);
|
bson_destroy (update);
|
bson_destroy (collation);
|
bson_destroy (&query);
|
mongoc_find_and_modify_opts_destroy (opts);
|
}
|
|
int main(void)
|
{
|
mongoc_collection_t *collection;
|
mongoc_database_t *database;
|
mongoc_client_t *client;
|
bson_error_t error;
|
|
mongoc_init();
|
client = mongoc_client_new ("mongodb://localhost:27017/admin?appname=find-and-modify-opts-example");
|
mongoc_client_set_error_api (client, 2);
|
database = mongoc_client_get_database (client, "databaseName");
|
|
collection = mongoc_database_create_collection (database, "collectionName", NULL, &error);
|
if (!collection) {
|
fprintf(stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
|
return 1;
|
}
|
|
fam_collation (collection);
|
|
mongoc_collection_drop (collection, NULL);
|
mongoc_database_destroy (database);
|
mongoc_collection_destroy (collection);
|
mongoc_client_destroy (client);
|
|
mongoc_cleanup ();
|
return 0;
|
}
|