1MONGOC_CREATE_INDEXES(3)           libmongoc          MONGOC_CREATE_INDEXES(3)
2
3
4

NAME

6       mongoc_create_indexes - Creating Indexes
7
8       To  create  indexes  on a MongoDB collection, execute the createIndexes
9       command  with  a  command  function   like   mongoc_database_write_com‐
10       mand_with_opts  or  mongoc_collection_write_command_with_opts.  See the
11       MongoDB Manual entry for the createIndexes command for details.
12
13       WARNING:
14          The commitQuorum option to the createIndexes command  is  only  sup‐
15          ported  in MongoDB 4.4+ servers, but it is not validated in the com‐
16          mand functions. Do not pass commitQuorum if connected to server ver‐
17          sions  less  than  4.4. Using the commitQuorum option on server ver‐
18          sions less than 4.4 may have adverse effects on index builds.
19

EXAMPLE

21       example-create-indexes.c
22
23          /* gcc example-create-indexes.c -o example-create-indexes $(pkg-config --cflags
24           * --libs libmongoc-1.0) */
25
26          /* ./example-create-indexes [CONNECTION_STRING [COLLECTION_NAME]] */
27
28          #include <mongoc/mongoc.h>
29          #include <stdio.h>
30          #include <stdlib.h>
31
32          int
33          main (int argc, char *argv[])
34          {
35             mongoc_client_t *client;
36             const char *uri_string =
37                "mongodb://127.0.0.1/?appname=create-indexes-example";
38             mongoc_uri_t *uri;
39             mongoc_database_t *db;
40             const char *collection_name = "test";
41             bson_t keys;
42             char *index_name;
43             bson_t *create_indexes;
44             bson_t reply;
45             char *reply_str;
46             bson_error_t error;
47             bool r;
48
49             mongoc_init ();
50
51             if (argc > 1) {
52                uri_string = argv[1];
53             }
54
55             if (argc > 2) {
56                collection_name = argv[2];
57             }
58
59             uri = mongoc_uri_new_with_error (uri_string, &error);
60             if (!uri) {
61                fprintf (stderr,
62                         "failed to parse URI: %s\n"
63                         "error message:       %s\n",
64                         uri_string,
65                         error.message);
66                return EXIT_FAILURE;
67             }
68
69             client = mongoc_client_new_from_uri (uri);
70             if (!client) {
71                return EXIT_FAILURE;
72             }
73
74             mongoc_client_set_error_api (client, 2);
75             db = mongoc_client_get_database (client, "test");
76
77             /* ascending index on field "x" */
78             bson_init (&keys);
79             BSON_APPEND_INT32 (&keys, "x", 1);
80             index_name = mongoc_collection_keys_to_index_string (&keys);
81             create_indexes = BCON_NEW ("createIndexes",
82                                        BCON_UTF8 (collection_name),
83                                        "indexes",
84                                        "[",
85                                        "{",
86                                        "key",
87                                        BCON_DOCUMENT (&keys),
88                                        "name",
89                                        BCON_UTF8 (index_name),
90                                        "}",
91                                        "]");
92
93             r = mongoc_database_write_command_with_opts (
94                db, create_indexes, NULL /* opts */, &reply, &error);
95
96             reply_str = bson_as_json (&reply, NULL);
97             printf ("%s\n", reply_str);
98
99             if (!r) {
100                fprintf (stderr, "Error in createIndexes: %s\n", error.message);
101             }
102
103             bson_free (index_name);
104             bson_free (reply_str);
105             bson_destroy (&reply);
106             bson_destroy (create_indexes);
107             mongoc_database_destroy (db);
108             mongoc_uri_destroy (uri);
109             mongoc_client_destroy (client);
110
111             mongoc_cleanup ();
112
113             return r ? EXIT_SUCCESS : EXIT_FAILURE;
114          }
115
116

AUTHOR

118       MongoDB, Inc
119
121       2017-present, MongoDB, Inc
122
123
124
125
1261.20.0                           Nov 18, 2021         MONGOC_CREATE_INDEXES(3)
Impressum