Docs Menu
Docs Home
/ /

Indexes

In this guide, you can learn how to use indexes with the C driver. Indexes can improve the efficiency of queries and add functionality to querying and storing documents.

Without indexes, MongoDB must scan every document in a collection to find the documents that match each query. These collection scans are slow and can negatively affect the performance of your application. However, if an appropriate index exists for a query, MongoDB can use the index to limit the documents it must inspect.

To improve query performance, build indexes on fields that appear often in your application's queries and operations that return sorted results. Each index that you add consumes disk space and memory when active, so we recommend that you track index memory and disk usage for capacity planning. In addition, when a write operation updates an indexed field, MongoDB updates the related index, which can negatively impact performance for write operations.

You can use wildcard indexes in your MongoDB application to query against fields whose names are not known in advance or are arbitrary. Wildcard indexes are not designed to replace workload-based index planning.

For more information about designing your data model and choosing indexes appropriate for your application, see the Data Modeling and Indexes guide in the MongoDB Server manual.

The examples in this guide use the movies and theaters collection in the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with Atlas guide.

MongoDB supports several different index types to help query your data. The following pages describe the most common index types and provide sample code for creating each index type.

  • Single-Field Indexes

  • Compound Indexes

  • Multikey Indexes

  • Atlas Search Indexes

The following sections describe other common index types and provide sample code for creating each index type.

The following example creates a 2dsphere index on the location.geo field that contains GeoJSON objects in the theaters collection:

bson_error_t error;
bson_t *keys = BCON_NEW("location.geo", BCON_UTF8("2dsphere"));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

To learn more about the GeoJSON data type, see GeoJSON Objects in the MongoDB Server manual.

The following example creates an ascending unique index on the title field:

bson_error_t error;
bson_t *keys = BCON_NEW("title", BCON_INT32(1));
bson_t *opts = BCON_NEW("unique", BCON_BOOL(true));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, opts);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
bson_destroy(opts);
mongoc_index_model_destroy(index_model);

The following example creates an ascending wildcard index in the specified collection:

bson_error_t error;
bson_t *keys = BCON_NEW("$**", BCON_INT32(1));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

The following example creates a new collection with an ascending clustered index on the _id field:

bson_error_t error;
bson_t *opts = BCON_NEW("clusteredIndex", "{",
"key", "{",
"_id", BCON_INT32(1),
"}",
"unique", BCON_BOOL(true),
"}");
mongoc_database_t *database = mongoc_client_get_database(client, "sample_mflix");
if (mongoc_database_create_collection(database, "clustered_collection", opts, &error)) {
printf("Successfully created collection\n");
} else {
fprintf(stderr, "Failed to create collection: %s", error.message);
}
mongoc_database_destroy(database);
bson_destroy(opts);

The following example creates a text index on the title string field:

bson_error_t error;
bson_t *keys = BCON_NEW("title", BCON_UTF8("text"));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

You can remove any unused index except the default unique index on the _id field. Pass a mongoc_collection_t structure, the index name, and an optional bson_error_t structure to the mongoc_collection_drop_index() function to remove an index from a collection.

The following example removes an index with the name "_title_" from the movies collection:

bson_error_t error;
if (mongoc_collection_drop_index(collection, "_title_", &error)) {
printf("Successfully dropped index\n");
} else {
fprintf(stderr, "Failed to drop index: %s", error.message);
}

Note

You cannot remove a single field from a compound text index. You must drop the entire index and create a new one to update the indexed fields.

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

Back

Time Series

On this page