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
10       mongoc_database_write_command_with_opts()                            or
11       mongoc_collection_write_command_with_opts(). See the MongoDB Manual en‐
12       try for the createIndexes command for details.
13
14       WARNING:
15          The  commitQuorum  option  to the createIndexes command is only sup‐
16          ported in MongoDB 4.4+ servers, but it is not validated in the  com‐
17          mand functions. Do not pass commitQuorum if connected to server ver‐
18          sions less than 4.4. Using the commitQuorum option  on  server  ver‐
19          sions less than 4.4 may have adverse effects on index builds.
20

EXAMPLE

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

AUTHOR

119       MongoDB, Inc
120
122       2017-present, MongoDB, Inc
123
124
125
126
1271.23.1                           Oct 20, 2022         MONGOC_CREATE_INDEXES(3)
Impressum