Work with Indexes
On this page
Overview
In this guide, you can learn how to use indexes with the C++ driver. Indexes can improve the efficiency of queries and add additional 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.
Operational Considerations
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 also updates any related index.
Because MongoDB supports dynamic schemas, applications can query against fields whose names are not known in advance or are arbitrary. MongoDB 4.2 introduced wildcard indexes to help support these queries. 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.
Sample Data
The examples in this guide use the sample_mflix.movies
collection
from the Atlas sample datasets. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see
Get Started with the C++ Driver.
Create an Index
MongoDB supports several different index types to support querying your data. The following sections describe the most common index types and provide sample code for creating each index type.
Single-Field Indexes
Single-field indexes are indexes with a
reference to a single field within a collection's
documents. They improve single-field query and sort performance, and support
TTL (time to live) indexes, which
automatically remove documents from a collection after a certain amount of time
or at a specific clock time. You can specify the sort order of the index entries
by specifying 1
for ascending or -1
for descending.
Note
The _id_
index is an example of a single-field index. This index is
automatically created on the _id
field when a new collection is created.
The following example creates a single-field index in ascending order on the title
field:
auto index_specification = make_document(kvp("title", 1)); collection.create_index(index_specification.view());
To learn more about single-field indexes, see Single Field Indexes in the MongoDB Server manual.
Remove an Index
You can remove any unused index except the default unique index on the
_id
field.
The following sections show how to remove a single index and how to remove all indexes in a collection.
Remove a Single Index
Pass an instance of an index or the index name to the drop_one()
method to
remove an index from a collection.
The following example removes an index with the name "title_1"
from the movies
collection:
collection.indexes().drop_one("title_1");
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.
Remove All Indexes
Starting with MongoDB 4.2, you can drop all indexes by calling the
drop_all()
method on the index view in your collection:
collection.indexes().drop_all();
For earlier versions of MongoDB, pass "*"
as a parameter to your call to
drop_one()
on the index view in your collection:
collection.indexes().drop_one("*");
Additional Information
To learn more about indexes in MongoDB, see the Indexes guide in the MongoDB Server manual.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: