Docs Menu
Docs Home
/ /

MongoDB Search Indexes

In this guide, you can learn how to programmatically manage your MongoDB Search and MongoDB Vector Search indexes by using the C driver.

The MongoDB Search feature enables you to perform full-text searches on collections hosted on MongoDB Atlas. To learn more about MongoDB Search, see the MongoDB Search Overview in the Atlas documentation.

MongoDB Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about MongoDB Vector Search, see the MongoDB Vector Search Overview in the Atlas documentation.

The following sections provide code examples that demonstrate how to create, list, update, and delete MongoDB Search and MongoDB Vector Search indexes.

To create a MongoDB Search or MongoDB Vector Search index, pass the createSearchIndexes command to the mongoc_collection_command_simple() function. You can use this command to create one or multiple indexes.

The following code example shows how to create a MongoDB Search index:

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"createSearchIndexes" : "%s",
"indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<index name>"} ]
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully created search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

The following code example shows how to create a MongoDB Vector Search index:

bson_t cmd;
bson_error_t error;
char * cmd_str = bson_strdup_printf(
BSON_STR({
"createSearchIndexes": "%s",
"indexes": [{
"name": "<index name>",
"type": "vectorSearch",
"definition": {
"fields": [{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "euclidean"
}]
}
}]
}),
"<collection name>"
);
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully created Vector Search index\n");
} else {
fprintf(stderr, "Failed to create Vector Search index: %s", error.message);
}
bson_destroy(&cmd);

The following code example shows how to create both search indexes in one call to the mongoc_collection_command_simple() function:

bson_t cmd;
bson_error_t error;
char * cmd_str = bson_strdup_printf(
BSON_STR({
"createSearchIndexes": "%s",
"indexes": [{
"definition": {
"mappings": {
"dynamic": false
}
},
"name": "<MongoDB Search index name>"
},
{
"name": "<Vector Search index name>",
"type": "vectorSearch",
"definition": {
"fields": [{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "euclidean"
}]
}
}
]
}),
"<collection name>");
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully created search indexes\n");
} else {
fprintf(stderr, "Failed to create search indexes: %s", error.message);
}
bson_destroy(&cmd);

To learn more about the syntax used to define each search index, see the following guides in the Atlas documentation:

You can pass the $listSearchIndexes aggregation stage to the mongoc_collection_aggregate() function to return all MongoDB Search and MongoDB Vector Search indexes in a collection.

The following code example shows how to print a list of the search indexes in a collection:

bson_t pipeline;
const bson_t *doc;
bson_error_t error;
const char *pipeline_str = BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
bson_init_from_json (&pipeline, pipeline_str, -1, &error);
mongoc_cursor_t *cursor =
mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (&pipeline);
mongoc_cursor_destroy (cursor);

You can pass the updateSearchIndex command to the mongoc_collection_command_simple() function to update a MongoDB Search or MongoDB Vector Search index.

The following code shows how to update the MongoDB Search index created in the Create a Search Index section of this guide to use dynamic mappings:

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"updateSearchIndex" : "%s",
"definition" : {"mappings" : {"dynamic" : true}}, "name" : "<index name>"}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully updated search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

The following code shows how to update the MongoDB Vector Search index created in the Create a Search Index section of this guide to use the cosine similarity function:

bson_t cmd;
bson_error_t error;
char * cmd_str = bson_strdup_printf(
BSON_STR({
"updateSearchIndex": "%s",
"name": "<index name>",
"definition": {
"fields": [{
"type": "vector",
"path": "plot_embedding",
"numDimensions": 1536,
"similarity": "cosine"
}]
}
}),
"<collection name>"
);
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully updated search index\n");
} else {
fprintf(stderr, "Failed to create search index: %s", error.message);
}
bson_destroy(&cmd);

You can pass the dropSearchIndexes command to the mongoc_collection_command_simple() function to delete a MongoDB Search or MongoDB Vector Search index.

The following code shows how to delete a search index from a collection:

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"dropSearchIndexes" : "%s",
"index" : "<index name>"
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully deleted search index\n");
} else {
fprintf (stderr, "Failed to delete search index: %s", error.message);
}
bson_destroy (&cmd);

To learn more about MongoDB Search, see MongoDB Search Indexes in the Atlas documentation.

To learn more about MongoDB Vector Search, see How to Index Fields for MongoDB Vector Search in the Atlas documentation.

To learn more about any of the functions discussed in this guide, see the following API documentation:

Back

Multikey Indexes

On this page