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

EXAMPLE

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

AUTHOR

111       MongoDB, Inc
112
114       2017-present, MongoDB, Inc
115
116
117
118
1191.16.2                           Feb 25, 2020         MONGOC_CREATE_INDEXES(3)
Impressum