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