1
2 void
fam_sort(mongoc_collection_t * collection)3 fam_sort (mongoc_collection_t *collection)
4 {
5 mongoc_find_and_modify_opts_t *opts;
6 bson_t *update;
7 bson_t sort = BSON_INITIALIZER;
8 bson_t reply;
9 bson_error_t error;
10 bson_t query = BSON_INITIALIZER;
11 bool success;
12
13
14 /* Find all users with the lastname Ibrahimovic */
15 BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
16
17 /* Sort by age (descending) */
18 BSON_APPEND_INT32 (&sort, "age", -1);
19
20 /* Bump his goal tally */
21 update = BCON_NEW ("$set", "{", "oldest", BCON_BOOL (true), "}");
22
23 opts = mongoc_find_and_modify_opts_new ();
24 mongoc_find_and_modify_opts_set_update (opts, update);
25 mongoc_find_and_modify_opts_set_sort (opts, &sort);
26
27 success = mongoc_collection_find_and_modify_with_opts (
28 collection, &query, opts, &reply, &error);
29
30 if (success) {
31 char *str;
32
33 str = bson_as_canonical_extended_json (&reply, NULL);
34 printf ("%s\n", str);
35 bson_free (str);
36 } else {
37 fprintf (
38 stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
39 }
40
41 bson_destroy (&reply);
42 bson_destroy (update);
43 bson_destroy (&sort);
44 bson_destroy (&query);
45 mongoc_find_and_modify_opts_destroy (opts);
46 }
47