Docs Menu
Docs Home
/ / /
C++ Driver
/

Work with Indexes

On this page

  • Overview
  • Operational Considerations
  • Sample Data
  • Create an Index
  • Remove an Index
  • Remove a Single Index
  • Remove All Indexes
  • Additional Information
  • API Documentation

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. If an appropriate index exists for a query, MongoDB can use the index to limit the documents it must inspect and improve potential query performance.

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.

The examples in this guide use the movies collection in the sample_mflix database from the Atlas sample datasets. To access this collection from your C++ application, instantiate a mongocxx::client that connects to an Atlas cluster and assign the following values to your db and collection variables:

auto db = client["sample_mflix"];
auto collection = db["movies"];

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 index types to help query your data. The following pages describe different index types and provide sample code to create each type of index:

  • Single Field Indexes

  • Compound Indexes

You can remove any unused index except the default unique index on the _id field.

The following sections provide examples that show how to remove one or more indexes from a collection.

To remove a single index from a collection, call the drop_one() method and pass in the index name or an instance of the index you want to remove.

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.

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("*");

To learn more about indexes in MongoDB, see the Indexes guide in the MongoDB Server manual.

To learn more about the methods or types discussed in this guide, see the following API documentation:

Back

Indexes