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

NAME

6       mongoc_find_and_modify_opts_set_sort       -       mongoc_find_and_mod‐
7       ify_opts_set_sort()
8

SYNOPSIS

10          bool
11          mongoc_find_and_modify_opts_set_sort (mongoc_find_and_modify_opts_t *opts,
12                                                const bson_t *sort);
13

PARAMETERS

15opts: A mongoc_find_and_modify_opts_t.
16
17sort: Determines which document the operation modifies if  the  query
18         selects multiple documents. findAndModify modifies the first document
19         in the sort order specified by this argument.
20

DESCRIPTION

22       Adds sort argument to the builder.
23
24       sort does not have to remain valid after calling this function.
25

RETURNS

27       Returns true if it successfully added the option to the builder, other‐
28       wise false.
29

SETTING SORT

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

AUTHOR

98       MongoDB, Inc
99
101       2017-present, MongoDB, Inc
102
103
104
105
1061.20.0                           Nov 18,MO2N0G2O1C_FIND_AND_MODIFY_OPTS_SET_SORT(3)
Impressum