1
2 void
fam_flags(mongoc_collection_t * collection)3 fam_flags (mongoc_collection_t *collection)
4 {
5 mongoc_find_and_modify_opts_t *opts;
6 bson_t reply;
7 bson_error_t error;
8 bson_t query = BSON_INITIALIZER;
9 bson_t *update;
10 bool success;
11
12
13 /* Find Zlatan Ibrahimovic, the striker */
14 BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
15 BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
16 BSON_APPEND_UTF8 (&query, "profession", "Football player");
17 BSON_APPEND_INT32 (&query, "age", 34);
18 BSON_APPEND_INT32 (
19 &query, "goals", (16 + 35 + 23 + 57 + 16 + 14 + 28 + 84) + (1 + 6 + 62));
20
21 /* Add his football position */
22 update = BCON_NEW ("$set", "{", "position", BCON_UTF8 ("striker"), "}");
23
24 opts = mongoc_find_and_modify_opts_new ();
25
26 mongoc_find_and_modify_opts_set_update (opts, update);
27
28 /* Create the document if it didn't exist, and return the updated document */
29 mongoc_find_and_modify_opts_set_flags (
30 opts, MONGOC_FIND_AND_MODIFY_UPSERT | MONGOC_FIND_AND_MODIFY_RETURN_NEW);
31
32 success = mongoc_collection_find_and_modify_with_opts (
33 collection, &query, opts, &reply, &error);
34
35 if (success) {
36 char *str;
37
38 str = bson_as_canonical_extended_json (&reply, NULL);
39 printf ("%s\n", str);
40 bson_free (str);
41 } else {
42 fprintf (
43 stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
44 }
45
46 bson_destroy (&reply);
47 bson_destroy (update);
48 bson_destroy (&query);
49 mongoc_find_and_modify_opts_destroy (opts);
50 }
51