1MONGOC_COLLECTION_FIND_AND_MODIFY(3)libmongoMcONGOC_COLLECTION_FIND_AND_MODIFY(3)
2
3
4

NAME

6       mongoc_collection_find_and_modify - mongoc_collection_find_and_modify()
7

SYNOPSIS

9          bool
10          mongoc_collection_find_and_modify (mongoc_collection_t *collection,
11                                             const bson_t *query,
12                                             const bson_t *sort,
13                                             const bson_t *update,
14                                             const bson_t *fields,
15                                             bool _remove,
16                                             bool upsert,
17                                             bool _new,
18                                             bson_t *reply,
19                                             bson_error_t *error);
20

PARAMETERS

22collection: A mongoc_collection_t.
23
24query: A bson_t containing the query to locate target document(s).
25
26sort: A bson_t containing the sort order for query.
27
28update: A bson_t containing an update spec.
29
30fields: An optional bson_t containing the fields to return or NULL.
31
32_remove: If the matching documents should be removed.
33
34upsert: If an upsert should be performed.
35
36_new: If the new version of the document should be returned.
37
38reply:  Optional pointer to an uninitialized bson_t that will be ini‐
39         tialized with the result.
40
41error: An optional location for a bson_error_t or NULL.
42

DESCRIPTION

44       Update and return an object.
45
46       This is a thin wrapper around the findAndModify command. Either  update
47       or _remove arguments are required.
48
49       As  of  MongoDB  3.2,  the mongoc_write_concern_t specified on the mon‐
50       goc_collection_t will be used, if any.
51
52       reply is always initialized, and must be freed with bson_destroy().
53

ERRORS

55       Errors are propagated via the error parameter.
56

RETURNS

58       Returns either the document before or after modification based  on  the
59       _new parameter.
60
61       A write concern timeout or write concern error is considered a failure.
62
63       SEE ALSO:
64          mongoc_collection_find_and_modify_with_opts.
65
66

EXAMPLE

68       find-and-modify.c
69
70          #include <mongoc/mongoc.h>
71          #include <stdio.h>
72
73
74          int
75          main (int argc, char *argv[])
76          {
77             mongoc_collection_t *collection;
78             mongoc_client_t *client;
79             const char *uri_string =
80                "mongodb://127.0.0.1:27017/?appname=find-and-modify-example";
81             mongoc_uri_t *uri;
82             bson_error_t error;
83             bson_t *query;
84             bson_t *update;
85             bson_t reply;
86             char *str;
87
88             mongoc_init ();
89
90             uri = mongoc_uri_new_with_error (uri_string, &error);
91             if (!uri) {
92                fprintf (stderr,
93                         "failed to parse URI: %s\n"
94                         "error message:       %s\n",
95                         uri_string,
96                         error.message);
97                return EXIT_FAILURE;
98             }
99
100             client = mongoc_client_new_from_uri (uri);
101             if (!client) {
102                return EXIT_FAILURE;
103             }
104
105             mongoc_client_set_error_api (client, 2);
106             collection = mongoc_client_get_collection (client, "test", "test");
107
108             /*
109              * Build our query, {"cmpxchg": 1}
110              */
111             query = BCON_NEW ("cmpxchg", BCON_INT32 (1));
112
113             /*
114              * Build our update. {"$set": {"cmpxchg": 2}}
115              */
116             update = BCON_NEW ("$set", "{", "cmpxchg", BCON_INT32 (2), "}");
117
118             /*
119              * Submit the findAndModify.
120              */
121             if (!mongoc_collection_find_and_modify (collection,
122                                                     query,
123                                                     NULL,
124                                                     update,
125                                                     NULL,
126                                                     false,
127                                                     false,
128                                                     true,
129                                                     &reply,
130                                                     &error)) {
131                fprintf (stderr, "find_and_modify() failure: %s\n", error.message);
132                return EXIT_FAILURE;
133             }
134
135             /*
136              * Print the result as JSON.
137              */
138             str = bson_as_canonical_extended_json (&reply, NULL);
139             printf ("%s\n", str);
140             bson_free (str);
141
142             /*
143              * Cleanup.
144              */
145             bson_destroy (query);
146             bson_destroy (update);
147             bson_destroy (&reply);
148             mongoc_collection_destroy (collection);
149             mongoc_uri_destroy (uri);
150             mongoc_client_destroy (client);
151
152             mongoc_cleanup ();
153
154             return EXIT_SUCCESS;
155          }
156
157

AUTHOR

159       MongoDB, Inc
160
162       2017-present, MongoDB, Inc
163
164
165
166
1671.20.0                           Nov 18, 20M2O1NGOC_COLLECTION_FIND_AND_MODIFY(3)
Impressum