1MONGOC_CLIENT_WRITE_COMMAND_WITH_OPTlSi(b3mM)oOnNgGoOcC_CLIENT_WRITE_COMMAND_WITH_OPTS(3)
2
3
4

SYNOPSIS

6          bool
7          mongoc_client_write_command_with_opts (mongoc_client_t *client,
8                                                 const char *db_name,
9                                                 const bson_t *command,
10                                                 const bson_t *opts,
11                                                 bson_t *reply,
12                                                 bson_error_t *error);
13
14       Execute  a  command  on  the server, applying logic that is specific to
15       commands that write, and taking the MongoDB  server  version  into  ac‐
16       count.  To  send a raw command to the server without any of this logic,
17       use mongoc_client_command_simple().
18
19       Use this function for commands that write such as  "drop"  or  "create‐
20       Role" (but not for "insert", "update", or "delete", see Basic Write Op‐
21       erations). Write concern and collation can  be  overridden  by  various
22       sources.  In  a transaction, read concern and write concern are prohib‐
23       ited in opts. The highest-priority sources for these options are listed
24       first  in the following table. The write concern is omitted for MongoDB
25       before 3.4.
26
27                             ┌──────────────┬───────────┐
28                             │Write Concern │ Collation │
29                             ├──────────────┼───────────┤
30opts          opts      
31                             ├──────────────┼───────────┤
32                             │Transaction   │           │
33                             ├──────────────┼───────────┤
34client        │           │
35                             └──────────────┴───────────┘
36
37       See the example for transactions and for the  "distinct"  command  with
38       opts.
39
40       reply is always initialized, and must be freed with bson_destroy().
41

PARAMETERS

43client: A mongoc_client_t.
44
45db_name: The name of the database to run the command on.
46
47command: A bson_t containing the command specification.
48
49opts: A bson_t containing additional options.
50
51reply: A location for the resulting document.
52
53error: An optional location for a bson_error_t or NULL.
54
55       opts may be NULL or a BSON document with additional command options:
56
57writeConcern:    Construct    a    mongoc_write_concern_t   and   use
58         mongoc_write_concern_append() to add the write concern to  opts.  See
59         the example code for mongoc_client_write_command_with_opts().
60
61sessionId:    First,   construct   a   mongoc_client_session_t   with
62         mongoc_client_start_session().  You  can  begin  a  transaction  with
63         mongoc_client_session_start_transaction(),    optionally    with    a
64         mongoc_transaction_opt_t that overrides the  options  inherited  from
65         client,  and use mongoc_client_session_append() to add the session to
66         opts. See the example code for mongoc_client_session_t.
67
68collation: Configure textual comparisons. See Setting  Collation  Or‐
69         der,  and  the  MongoDB Manual entry on Collation. Collation requires
70         MongoDB 3.2 or later, otherwise an error is returned.
71
72serverId: To target a specific server, include  an  int32  "serverId"
73         field.  Obtain  the id by calling mongoc_client_select_server(), then
74         mongoc_server_description_id() on its return value.
75
76       Consult the MongoDB Manual entry on Database  Commands  for  each  com‐
77       mand's arguments.
78

ERRORS

80       Errors are propagated via the error parameter.
81

RETURNS

83       Returns  true  if successful. Returns false and sets error if there are
84       invalid arguments or a server or network error.
85
86       A write concern timeout or write concern error is considered a failure.
87

BASIC WRITE OPERATIONS

89       Do not use this function to call the  basic  write  commands  "insert",
90       "update", and "delete". Those commands require special logic not imple‐
91       mented in mongoc_client_write_command_with_opts. For basic write opera‐
92       tions use CRUD functions such as mongoc_collection_insert_one() and the
93       others described in the CRUD tutorial, or use the Bulk API.
94

EXAMPLE

96       example-command-with-opts.c
97
98          /*
99
100          Demonstrates how to prepare options for mongoc_client_read_command_with_opts and
101          mongoc_client_write_command_with_opts. First it calls "cloneCollectionAsCapped"
102          command with "writeConcern" option, then "distinct" command with "collation" and
103          "readConcern" options,
104
105          Start a MongoDB 3.4 replica set with --enableMajorityReadConcern and insert two
106          documents:
107
108          $ mongo
109          MongoDB Enterprise replset:PRIMARY> db.my_collection.insert({x: 1, y: "One"})
110          WriteResult({ "nInserted" : 1 })
111          MongoDB Enterprise replset:PRIMARY> db.my_collection.insert({x: 2, y: "Two"})
112          WriteResult({ "nInserted" : 1 })
113
114          Build and run the example:
115
116          gcc example-command-with-opts.c -o example-command-with-opts $(pkg-config
117          --cflags --libs libmongoc-1.0)
118          ./example-command-with-opts [CONNECTION_STRING]
119          cloneCollectionAsCapped: { "ok" : 1 }
120          distinct: { "values" : [ 1, 2 ], "ok" : 1 }
121
122          */
123
124          #include <mongoc/mongoc.h>
125          #include <stdio.h>
126          #include <stdlib.h>
127
128          int
129          main (int argc, char *argv[])
130          {
131             mongoc_client_t *client;
132             const char *uri_string = "mongodb://127.0.0.1/?appname=client-example";
133             mongoc_uri_t *uri;
134             bson_t *cmd;
135             bson_t *opts;
136             mongoc_write_concern_t *write_concern;
137             mongoc_read_prefs_t *read_prefs;
138             mongoc_read_concern_t *read_concern;
139             bson_t reply;
140             bson_error_t error;
141             char *json;
142
143             mongoc_init ();
144
145             if (argc > 1) {
146                uri_string = argv[1];
147             }
148
149             uri = mongoc_uri_new_with_error (uri_string, &error);
150             if (!uri) {
151                fprintf (stderr,
152                         "failed to parse URI: %s\n"
153                         "error message:       %s\n",
154                         uri_string,
155                         error.message);
156                return EXIT_FAILURE;
157             }
158
159             client = mongoc_client_new_from_uri (uri);
160             if (!client) {
161                return EXIT_FAILURE;
162             }
163
164             mongoc_client_set_error_api (client, 2);
165
166             cmd = BCON_NEW ("cloneCollectionAsCapped",
167                             BCON_UTF8 ("my_collection"),
168                             "toCollection",
169                             BCON_UTF8 ("my_capped_collection"),
170                             "size",
171                             BCON_INT64 (1024 * 1024));
172
173             /* include write concern "majority" in command options */
174             write_concern = mongoc_write_concern_new ();
175             mongoc_write_concern_set_wmajority (write_concern, 10000 /* wtimeoutMS */);
176             opts = bson_new ();
177             mongoc_write_concern_append (write_concern, opts);
178
179             if (mongoc_client_write_command_with_opts (
180                    client, "test", cmd, opts, &reply, &error)) {
181                json = bson_as_canonical_extended_json (&reply, NULL);
182                printf ("cloneCollectionAsCapped: %s\n", json);
183                bson_free (json);
184             } else {
185                fprintf (stderr, "cloneCollectionAsCapped: %s\n", error.message);
186             }
187
188             bson_free (cmd);
189             bson_free (opts);
190
191             /* distinct values of "x" in "my_collection" where "y" sorts after "one" */
192             cmd = BCON_NEW ("distinct",
193                             BCON_UTF8 ("my_collection"),
194                             "key",
195                             BCON_UTF8 ("x"),
196                             "query",
197                             "{",
198                             "y",
199                             "{",
200                             "$gt",
201                             BCON_UTF8 ("one"),
202                             "}",
203                             "}");
204
205             read_prefs = mongoc_read_prefs_new (MONGOC_READ_SECONDARY);
206
207             /* "One" normally sorts before "one"; make "One" sort after "one" */
208             opts = BCON_NEW ("collation",
209                              "{",
210                              "locale",
211                              BCON_UTF8 ("en_US"),
212                              "caseFirst",
213                              BCON_UTF8 ("lower"),
214                              "}");
215
216             /* add a read concern to "opts" */
217             read_concern = mongoc_read_concern_new ();
218             mongoc_read_concern_set_level (read_concern,
219                                            MONGOC_READ_CONCERN_LEVEL_MAJORITY);
220
221             mongoc_read_concern_append (read_concern, opts);
222
223             if (mongoc_client_read_command_with_opts (
224                    client, "test", cmd, read_prefs, opts, &reply, &error)) {
225                json = bson_as_canonical_extended_json (&reply, NULL);
226                printf ("distinct: %s\n", json);
227                bson_free (json);
228             } else {
229                fprintf (stderr, "distinct: %s\n", error.message);
230             }
231
232             bson_destroy (cmd);
233             bson_destroy (opts);
234             bson_destroy (&reply);
235             mongoc_read_prefs_destroy (read_prefs);
236             mongoc_read_concern_destroy (read_concern);
237             mongoc_write_concern_destroy (write_concern);
238             mongoc_uri_destroy (uri);
239             mongoc_client_destroy (client);
240
241             mongoc_cleanup ();
242
243             return EXIT_SUCCESS;
244          }
245
246

AUTHOR

248       MongoDB, Inc
249
251       2017-present, MongoDB, Inc
252
253
254
255
2561.25.1                           Nov 08M,ON2G0O2C3_CLIENT_WRITE_COMMAND_WITH_OPTS(3)
Impressum