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       See also: mongoc_collection_find_and_modify_with_opts.
50
51       As  of  MongoDB  3.2,  the mongoc_write_concern_t specified on the mon‐
52       goc_collection_t will be used, if any.
53
54       reply is always initialized, and must be freed with bson_destroy().
55

ERRORS

57       Errors are propagated via the error parameter.
58

RETURNS

60       Returns either the document before or after modification based  on  the
61       _new parameter.
62
63       A write concern timeout or write concern error is considered a failure.
64

EXAMPLE

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

AUTHOR

157       MongoDB, Inc
158
160       2017-present, MongoDB, Inc
161
162
163
164
1651.17.6                           Jun 03, 20M2O1NGOC_COLLECTION_FIND_AND_MODIFY(3)
Impressum