1MONGOC_MANAGE_COLLECTION_INDEXES(3)libmongocMONGOC_MANAGE_COLLECTION_INDEXES(3)
2
3
4
5To create indexes on a MongoDB collection, use
7
8 // `keys` represents an ascending index on field `x`.
9 bson_t *keys = BCON_NEW ("x", BCON_INT32 (1));
10 mongoc_index_model_t *im = mongoc_index_model_new (keys, NULL /* opts */);
11 if (mongoc_collection_create_indexes_with_opts (
12 coll, &im, 1, NULL /* opts */, NULL /* reply */, &error)) {
13 printf ("Successfully created index\n");
14 } else {
15 bson_destroy (keys);
16 HANDLE_ERROR ("Failed to create index: %s", error.message);
17 }
18 bson_destroy (keys);
19
20
21 To list indexes, use mongoc_collection_find_indexes_with_opts():
22
23 mongoc_cursor_t *cursor =
24 mongoc_collection_find_indexes_with_opts (coll, NULL /* opts */);
25 printf ("Listing indexes:\n");
26 const bson_t *got;
27 while (mongoc_cursor_next (cursor, &got)) {
28 char *got_str = bson_as_canonical_extended_json (got, NULL);
29 printf (" %s\n", got_str);
30 bson_free (got_str);
31 }
32 if (mongoc_cursor_error (cursor, &error)) {
33 mongoc_cursor_destroy (cursor);
34 HANDLE_ERROR ("Failed to list indexes: %s", error.message);
35 }
36 mongoc_cursor_destroy (cursor);
37
38
39 To drop an index, use mongoc_collection_drop_index_with_opts(). The in‐
40 dex name may be obtained from the keys document with
41 mongoc_collection_keys_to_index_string():
42
43 bson_t *keys = BCON_NEW ("x", BCON_INT32 (1));
44 char *index_name = mongoc_collection_keys_to_index_string (keys);
45 if (mongoc_collection_drop_index_with_opts (
46 coll, index_name, NULL /* opts */, &error)) {
47 printf ("Successfully dropped index\n");
48 } else {
49 bson_free (index_name);
50 bson_destroy (keys);
51 HANDLE_ERROR ("Failed to drop index: %s", error.message);
52 }
53 bson_free (index_name);
54 bson_destroy (keys);
55
56
57 For a full example, see example-manage-collection-indexes.c.
58
60 To create an Atlas Search Index, use the createSearchIndexes command:
61
62 bson_t cmd;
63 // Create command.
64 {
65 char *cmd_str = bson_strdup_printf (
66 BSON_STR ({
67 "createSearchIndexes" : "%s",
68 "indexes" : [ {
69 "definition" : {"mappings" : {"dynamic" : false}},
70 "name" : "test-index"
71 } ]
72 }),
73 collname);
74 ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error));
75 bson_free (cmd_str);
76 }
77 if (!mongoc_collection_command_simple (
78 coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) {
79 bson_destroy (&cmd);
80 HANDLE_ERROR ("Failed to run createSearchIndexes: %s", error.message);
81 }
82 printf ("Created index: \"test-index\"\n");
83 bson_destroy (&cmd);
84
85
86 To list Atlas Search Indexes, use the $listSearchIndexes aggregation
87 stage:
88
89 const char *pipeline_str =
90 BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
91 bson_t pipeline;
92 ASSERT (bson_init_from_json (&pipeline, pipeline_str, -1, &error));
93 mongoc_cursor_t *cursor =
94 mongoc_collection_aggregate (coll,
95 MONGOC_QUERY_NONE,
96 &pipeline,
97 NULL /* opts */,
98 NULL /* read_prefs */);
99 printf ("Listing indexes:\n");
100 const bson_t *got;
101 while (mongoc_cursor_next (cursor, &got)) {
102 char *got_str = bson_as_canonical_extended_json (got, NULL);
103 printf (" %s\n", got_str);
104 bson_free (got_str);
105 }
106 if (mongoc_cursor_error (cursor, &error)) {
107 bson_destroy (&pipeline);
108 mongoc_cursor_destroy (cursor);
109 HANDLE_ERROR ("Failed to run $listSearchIndexes: %s", error.message);
110 }
111 bson_destroy (&pipeline);
112 mongoc_cursor_destroy (cursor);
113
114
115 To update an Atlas Search Index, use the updateSearchIndex command:
116
117 bson_t cmd;
118 // Create command.
119 {
120 char *cmd_str = bson_strdup_printf (
121 BSON_STR ({
122 "updateSearchIndex" : "%s",
123 "definition" : {"mappings" : {"dynamic" : true}},
124 "name" : "test-index"
125 }),
126 collname);
127 ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error));
128 bson_free (cmd_str);
129 }
130 if (!mongoc_collection_command_simple (
131 coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) {
132 bson_destroy (&cmd);
133 HANDLE_ERROR ("Failed to run updateSearchIndex: %s", error.message);
134 }
135 printf ("Updated index: \"test-index\"\n");
136 bson_destroy (&cmd);
137
138
139 To drop an Atlas Search Index, use the dropSearchIndex command:
140
141 bson_t cmd;
142 // Create command.
143 {
144 char *cmd_str = bson_strdup_printf (
145 BSON_STR ({"dropSearchIndex" : "%s", "name" : "test-index"}),
146 collname);
147 ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error));
148 bson_free (cmd_str);
149 }
150 if (!mongoc_collection_command_simple (
151 coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) {
152 bson_destroy (&cmd);
153 HANDLE_ERROR ("Failed to run dropSearchIndex: %s", error.message);
154 }
155 printf ("Dropped index: \"test-index\"\n");
156 bson_destroy (&cmd);
157
158
159 For a full example, see example-manage-search-indexes.c.
160
162 MongoDB, Inc
163
165 2017-present, MongoDB, Inc
166
167
168
169
1701.25.1 Nov 08, 202M3ONGOC_MANAGE_COLLECTION_INDEXES(3)