1MONGOC_FIND_AND_MODIFY_OPTS_SET_SORTl(i3b)moMnOgNoGcOC_FIND_AND_MODIFY_OPTS_SET_SORT(3)
2
3
4

SYNOPSIS

6          bool
7          mongoc_find_and_modify_opts_set_sort (mongoc_find_and_modify_opts_t *opts,
8                                                const bson_t *sort);
9

PARAMETERS

11opts: A mongoc_find_and_modify_opts_t.
12
13sort:  Determines  which document the operation modifies if the query
14         selects multiple documents. findAndModify modifies the first document
15         in the sort order specified by this argument.
16

DESCRIPTION

18       Adds sort argument to the builder.
19
20       sort does not have to remain valid after calling this function.
21

RETURNS

23       Returns true if it successfully added the option to the builder, other‐
24       wise false.
25

SETTING SORT

27       sort.c
28
29          void
30          fam_sort (mongoc_collection_t *collection)
31          {
32             mongoc_find_and_modify_opts_t *opts;
33             bson_t *update;
34             bson_t sort = BSON_INITIALIZER;
35             bson_t reply;
36             bson_error_t error;
37             bson_t query = BSON_INITIALIZER;
38             bool success;
39
40
41             /* Find all users with the lastname Ibrahimovic */
42             BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
43
44             /* Sort by age (descending) */
45             BSON_APPEND_INT32 (&sort, "age", -1);
46
47             /* Bump his goal tally */
48             update = BCON_NEW ("$set", "{", "oldest", BCON_BOOL (true), "}");
49
50             opts = mongoc_find_and_modify_opts_new ();
51             mongoc_find_and_modify_opts_set_update (opts, update);
52             mongoc_find_and_modify_opts_set_sort (opts, &sort);
53
54             success = mongoc_collection_find_and_modify_with_opts (
55                collection, &query, opts, &reply, &error);
56
57             if (success) {
58                char *str;
59
60                str = bson_as_canonical_extended_json (&reply, NULL);
61                printf ("%s\n", str);
62                bson_free (str);
63             } else {
64                fprintf (
65                   stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
66             }
67
68             bson_destroy (&reply);
69             bson_destroy (update);
70             bson_destroy (&sort);
71             bson_destroy (&query);
72             mongoc_find_and_modify_opts_destroy (opts);
73          }
74
75
76       Outputs:
77
78          {
79             "lastErrorObject" : {"updatedExisting" : true, "n" : 1},
80                                 "value" : {
81                                    "_id" : {"$oid" : "56562a99d13e6d86239c7b00"},
82                                    "age" : 35,
83                                    "firstname" : "Zlatan",
84                                    "goals" : 343,
85                                    "lastname" : "Ibrahimovic",
86                                    "profession" : "Football player",
87                                    "position" : "striker",
88                                    "author" : true
89                                 },
90                                           "ok" : 1
91          }
92

AUTHOR

94       MongoDB, Inc
95
97       2017-present, MongoDB, Inc
98
99
100
101
1021.25.1                           Nov 08,MO2N0G2O3C_FIND_AND_MODIFY_OPTS_SET_SORT(3)
Impressum