1MONGOC_CLIENT_READ_COMMAND_WITH_OPTSl(i3b)moMnOgNoGcOC_CLIENT_READ_COMMAND_WITH_OPTS(3)
2
3
4
6 bool
7 mongoc_client_read_command_with_opts (mongoc_client_t *client,
8 const char *db_name,
9 const bson_t *command,
10 const mongoc_read_prefs_t *read_prefs,
11 const bson_t *opts,
12 bson_t *reply,
13 bson_error_t *error);
14
15 Execute a command on the server, applying logic that is specific to
16 commands that read, and taking the MongoDB server version into account.
17 To send a raw command to the server without any of this logic, use
18 mongoc_client_command_simple().
19
20 Use this function for commands that read such as "count" or "distinct".
21
22 Read preferences, read concern, and collation can be overridden by var‐
23 ious sources. In a transaction, read concern and write concern are pro‐
24 hibited in opts and the read preference must be primary or NULL. The
25 highest-priority sources for these options are listed first in the fol‐
26 lowing table. No write concern is applied.
27
28 ┌─────────────────┬──────────────┬───────────┐
29 │Read Preferences │ Read Concern │ Collation │
30 ├─────────────────┼──────────────┼───────────┤
31 │read_prefs │ opts │ opts │
32 ├─────────────────┼──────────────┼───────────┤
33 │Transaction │ Transaction │ │
34 ├─────────────────┼──────────────┼───────────┤
35 │client │ │ │
36 └─────────────────┴──────────────┴───────────┘
37
38 See the example for transactions and for the "distinct" command with
39 opts.
40
41 reply is always initialized, and must be freed with bson_destroy().
42
43 This function is considered a retryable read operation. Upon a tran‐
44 sient error (a network error, errors due to replica set failover, etc.)
45 the operation is safely retried once. If retryreads is false in the
46 URI (see mongoc_uri_t) the retry behavior does not apply.
47
48 Retry logic occurs regardless of the underlying command. Retrying
49 mapReduce has the potential for degraded performance. Retrying a get‐
50 More command has the potential to miss results. For those commands, use
51 generic command helpers (like mongoc_client_command_with_opts()) in‐
52 stead.
53
55 • client: A mongoc_client_t.
56
57 • db_name: The name of the database to run the command on.
58
59 • command: A bson_t containing the command specification.
60
61 • read_prefs: An optional mongoc_read_prefs_t.
62
63 • opts: A bson_t containing additional options.
64
65 • reply: A location for the resulting document.
66
67 • error: An optional location for a bson_error_t or NULL.
68
69 opts may be NULL or a BSON document with additional command options:
70
71 • readConcern: Construct a mongoc_read_concern_t and use
72 mongoc_read_concern_append() to add the read concern to opts. See the
73 example code for mongoc_client_read_command_with_opts(). Read concern
74 requires MongoDB 3.2 or later, otherwise an error is returned.
75
76 • sessionId: First, construct a mongoc_client_session_t with
77 mongoc_client_start_session(). You can begin a transaction with
78 mongoc_client_session_start_transaction(), optionally with a
79 mongoc_transaction_opt_t that overrides the options inherited from
80 client, and use mongoc_client_session_append() to add the session to
81 opts. See the example code for mongoc_client_session_t.
82
83 • collation: Configure textual comparisons. See Setting Collation Or‐
84 der, and the MongoDB Manual entry on Collation. Collation requires
85 MongoDB 3.2 or later, otherwise an error is returned.
86
87 • serverId: To target a specific server, include an int32 "serverId"
88 field. Obtain the id by calling mongoc_client_select_server(), then
89 mongoc_server_description_id() on its return value.
90
91 Consult the MongoDB Manual entry on Database Commands for each com‐
92 mand's arguments.
93
95 Errors are propagated via the error parameter.
96
98 Returns true if successful. Returns false and sets error if there are
99 invalid arguments or a server or network error.
100
102 example-command-with-opts.c
103
104 /*
105
106 Demonstrates how to prepare options for mongoc_client_read_command_with_opts and
107 mongoc_client_write_command_with_opts. First it calls "cloneCollectionAsCapped"
108 command with "writeConcern" option, then "distinct" command with "collation" and
109 "readConcern" options,
110
111 Start a MongoDB 3.4 replica set with --enableMajorityReadConcern and insert two
112 documents:
113
114 $ mongo
115 MongoDB Enterprise replset:PRIMARY> db.my_collection.insert({x: 1, y: "One"})
116 WriteResult({ "nInserted" : 1 })
117 MongoDB Enterprise replset:PRIMARY> db.my_collection.insert({x: 2, y: "Two"})
118 WriteResult({ "nInserted" : 1 })
119
120 Build and run the example:
121
122 gcc example-command-with-opts.c -o example-command-with-opts $(pkg-config
123 --cflags --libs libmongoc-1.0)
124 ./example-command-with-opts [CONNECTION_STRING]
125 cloneCollectionAsCapped: { "ok" : 1 }
126 distinct: { "values" : [ 1, 2 ], "ok" : 1 }
127
128 */
129
130 #include <mongoc/mongoc.h>
131 #include <stdio.h>
132 #include <stdlib.h>
133
134 int
135 main (int argc, char *argv[])
136 {
137 mongoc_client_t *client;
138 const char *uri_string = "mongodb://127.0.0.1/?appname=client-example";
139 mongoc_uri_t *uri;
140 bson_t *cmd;
141 bson_t *opts;
142 mongoc_write_concern_t *write_concern;
143 mongoc_read_prefs_t *read_prefs;
144 mongoc_read_concern_t *read_concern;
145 bson_t reply;
146 bson_error_t error;
147 char *json;
148
149 mongoc_init ();
150
151 if (argc > 1) {
152 uri_string = argv[1];
153 }
154
155 uri = mongoc_uri_new_with_error (uri_string, &error);
156 if (!uri) {
157 fprintf (stderr,
158 "failed to parse URI: %s\n"
159 "error message: %s\n",
160 uri_string,
161 error.message);
162 return EXIT_FAILURE;
163 }
164
165 client = mongoc_client_new_from_uri (uri);
166 if (!client) {
167 return EXIT_FAILURE;
168 }
169
170 mongoc_client_set_error_api (client, 2);
171
172 cmd = BCON_NEW ("cloneCollectionAsCapped",
173 BCON_UTF8 ("my_collection"),
174 "toCollection",
175 BCON_UTF8 ("my_capped_collection"),
176 "size",
177 BCON_INT64 (1024 * 1024));
178
179 /* include write concern "majority" in command options */
180 write_concern = mongoc_write_concern_new ();
181 mongoc_write_concern_set_wmajority (write_concern, 10000 /* wtimeoutMS */);
182 opts = bson_new ();
183 mongoc_write_concern_append (write_concern, opts);
184
185 if (mongoc_client_write_command_with_opts (
186 client, "test", cmd, opts, &reply, &error)) {
187 json = bson_as_canonical_extended_json (&reply, NULL);
188 printf ("cloneCollectionAsCapped: %s\n", json);
189 bson_free (json);
190 } else {
191 fprintf (stderr, "cloneCollectionAsCapped: %s\n", error.message);
192 }
193
194 bson_free (cmd);
195 bson_free (opts);
196
197 /* distinct values of "x" in "my_collection" where "y" sorts after "one" */
198 cmd = BCON_NEW ("distinct",
199 BCON_UTF8 ("my_collection"),
200 "key",
201 BCON_UTF8 ("x"),
202 "query",
203 "{",
204 "y",
205 "{",
206 "$gt",
207 BCON_UTF8 ("one"),
208 "}",
209 "}");
210
211 read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);
212
213 /* "One" normally sorts before "one"; make "One" sort after "one" */
214 opts = BCON_NEW ("collation",
215 "{",
216 "locale",
217 BCON_UTF8 ("en_US"),
218 "caseFirst",
219 BCON_UTF8 ("lower"),
220 "}");
221
222 /* add a read concern to "opts" */
223 read_concern = mongoc_read_concern_new ();
224 mongoc_read_concern_set_level (read_concern,
225 MONGOC_READ_CONCERN_LEVEL_MAJORITY);
226
227 mongoc_read_concern_append (read_concern, opts);
228
229 if (mongoc_client_read_command_with_opts (
230 client, "test", cmd, read_prefs, opts, &reply, &error)) {
231 json = bson_as_canonical_extended_json (&reply, NULL);
232 printf ("distinct: %s\n", json);
233 bson_free (json);
234 } else {
235 fprintf (stderr, "distinct: %s\n", error.message);
236 }
237
238 bson_destroy (cmd);
239 bson_destroy (opts);
240 bson_destroy (&reply);
241 mongoc_read_prefs_destroy (read_prefs);
242 mongoc_read_concern_destroy (read_concern);
243 mongoc_write_concern_destroy (write_concern);
244 mongoc_uri_destroy (uri);
245 mongoc_client_destroy (client);
246
247 mongoc_cleanup ();
248
249 return EXIT_SUCCESS;
250 }
251
252
254 MongoDB, Inc
255
257 2017-present, MongoDB, Inc
258
259
260
261
2621.25.1 Nov 08,MO2N0G2O3C_CLIENT_READ_COMMAND_WITH_OPTS(3)