MONGOC_MANAGE_...ECTION_INDEXES(3)
π MANAGE COLLECTION INDEXES π
okay bestie, so this page lives in the libmongoc manual, and it's your whole guide to wrangling indexes on a MongoDB collection β creating them, listing them, dropping them, and then a whole separate vibe for Atlas Search indexes. let's go π
β¨ CREATE AN INDEX β¨
to create indexes on a MongoDB collection, you use mongoc_collection_create_indexes_with_opts():
// `keys` represents an ascending index on field `x`.
bson_t *keys = BCON_NEW ("x", BCON_INT32 (1));
mongoc_index_model_t *im = mongoc_index_model_new (keys, NULL /* opts */);
if (mongoc_collection_create_indexes_with_opts (
coll, &im, 1, NULL /* opts */, NULL /* reply */, &error)) {
printf ("Successfully created index\n");
} else {
bson_destroy (keys);
HANDLE_ERROR ("Failed to create index: %s", error.message);
}
bson_destroy (keys);
side note for real:keysis an ascending index on the fieldx. that's theBCON_INT32 (1)doing its thing π
π LIST YOUR INDEXES π
to list indexes, hit up mongoc_collection_find_indexes_with_opts():
mongoc_cursor_t *cursor =
mongoc_collection_find_indexes_with_opts (coll, NULL /* opts */);
printf ("Listing indexes:\n");
const bson_t *got;
while (mongoc_cursor_next (cursor, &got)) {
char *got_str = bson_as_canonical_extended_json (got, NULL);
printf (" %s\n", got_str);
bson_free (got_str);
}
if (mongoc_cursor_error (cursor, &error)) {
mongoc_cursor_destroy (cursor);
HANDLE_ERROR ("Failed to list indexes: %s", error.message);
}
mongoc_cursor_destroy (cursor);
ποΈ DROP AN INDEX ποΈ
to drop an index, use mongoc_collection_drop_index_with_opts(). and where does the index name come from?? bestie, you get it from the keys document with mongoc_collection_keys_to_index_string() π«Ά
bson_t *keys = BCON_NEW ("x", BCON_INT32 (1));
char *index_name = mongoc_collection_keys_to_index_string (keys);
if (mongoc_collection_drop_index_with_opts (
coll, index_name, NULL /* opts */, &error)) {
printf ("Successfully dropped index\n");
} else {
bson_free (index_name);
bson_destroy (keys);
HANDLE_ERROR ("Failed to drop index: %s", error.message);
}
bson_free (index_name);
bson_destroy (keys);
and if you want the full example, go peek at example-manage-collection-indexes.c β it's giving complete, no cap β¨
π MANAGE ATLAS SEARCH INDEXES π
okay now we're in a whole different era, this is the Atlas Search Index saga π¦
π CREATE π
to create an Atlas Search Index, use the createSearchIndexes command:
bson_t cmd;
// Create command.
{
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"createSearchIndexes" : "%s",
"indexes" : [ {
"definition" : {"mappings" : {"dynamic" : false}},
"name" : "test-index"
} ]
}),
collname);
ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error));
bson_free (cmd_str);
}
if (!mongoc_collection_command_simple (
coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) {
bson_destroy (&cmd);
HANDLE_ERROR ("Failed to run createSearchIndexes: %s", error.message);
}
printf ("Created index: \"test-index\"\n");
bson_destroy (&cmd);
πΈ LIST πΈ
to list Atlas Search Indexes, use the $listSearchIndexes aggregation stage:
const char *pipeline_str =
BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
bson_t pipeline;
ASSERT (bson_init_from_json (&pipeline, pipeline_str, -1, &error));
mongoc_cursor_t *cursor =
mongoc_collection_aggregate (coll,
MONGOC_QUERY_NONE,
&pipeline,
NULL /* opts */,
NULL /* read_prefs */);
printf ("Listing indexes:\n");
const bson_t *got;
while (mongoc_cursor_next (cursor, &got)) {
char *got_str = bson_as_canonical_extended_json (got, NULL);
printf (" %s\n", got_str);
bson_free (got_str);
}
if (mongoc_cursor_error (cursor, &error)) {
bson_destroy (&pipeline);
mongoc_cursor_destroy (cursor);
HANDLE_ERROR ("Failed to run $listSearchIndexes: %s", error.message);
}
bson_destroy (&pipeline);
mongoc_cursor_destroy (cursor);
β UPDATE β
to update an Atlas Search Index, use the updateSearchIndex command:
bson_t cmd;
// Create command.
{
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"updateSearchIndex" : "%s",
"definition" : {"mappings" : {"dynamic" : true}},
"name" : "test-index"
}),
collname);
ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error));
bson_free (cmd_str);
}
if (!mongoc_collection_command_simple (
coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) {
bson_destroy (&cmd);
HANDLE_ERROR ("Failed to run updateSearchIndex: %s", error.message);
}
printf ("Updated index: \"test-index\"\n");
bson_destroy (&cmd);
π§ DROP π§
to drop an Atlas Search Index, use the dropSearchIndex command:
bson_t cmd;
// Create command.
{
char *cmd_str = bson_strdup_printf (
BSON_STR ({"dropSearchIndex" : "%s", "name" : "test-index"}),
collname);
ASSERT (bson_init_from_json (&cmd, cmd_str, -1, &error));
bson_free (cmd_str);
}
if (!mongoc_collection_command_simple (
coll, &cmd, NULL /* read_prefs */, NULL /* reply */, &error)) {
bson_destroy (&cmd);
HANDLE_ERROR ("Failed to run dropSearchIndex: %s", error.message);
}
printf ("Dropped index: \"test-index\"\n");
bson_destroy (&cmd);
for the whole full example, go read example-manage-search-indexes.c β it's iconic, iykyk π
π AUTHOR π
MongoDB, Inc
π COPYRIGHT π
2017-present, MongoDB, Inc
and that's the page bestie, we ate π the version on this one is 1.26.0, dated Mar 31, 2025, and it's filed under libmongoc in the MONGOC_MANAGE_...ECTION_INDEXES(3) slot. slay, go index something πβ¨
π¦ where this page comes from
- ubuntu noble: libmongoc-doc 1.26.0-1.1ubuntu2 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms