Optimize Queries with Indexes
On this page
Overview
On this page, you can see copyable code examples that demonstrate how to use the C++ driver to work with common types of indexes.
Tip
To learn more about working with indexes, see the Work with Indexes guide. To learn more about any of the indexes shown on this page, see the link provided in each section.
To use an example from this page, copy the code example into the
sample application or your own application.
Be sure to replace all placeholders in the code examples, such as <connection string>
, with
the relevant values for your MongoDB deployment.
Sample Application
You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:
Ensure you have the C++ driver installed in a location from which your project can import it.
Copy the following code and paste it into a new
.cpp
file within your project.Copy a code example from this page and paste it within the highlighted section of the file.
1 2 3 4 5 6 7 8 9 10 using bsoncxx::builder::basic::kvp; 11 using bsoncxx::builder::basic::make_document; 12 13 int main() { 14 try { 15 mongocxx::instance instance; 16 17 mongocxx::uri uri("<connection string>"); 18 mongocxx::client client(uri); 19 20 auto database = client["<database name>"]; 21 auto collection = database["<collection name>"]; 22 23 // Start example code here 24 25 // End example code here 26 27 } catch (const mongocxx::exception& e) { 28 std::cout << "An exception occurred: " << e.what() << "\n"; 29 return EXIT_FAILURE; 30 } 31 32 return EXIT_SUCCESS; 33 }
Single-Field Index
The following code shows how to create an ascending single-field index:
auto index_specification = make_document(kvp("<field name>", 1)); auto result = collection.create_index(index_specification.view()); std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
Index created: { "name" : "<field name>_1" }
To learn more about single-field indexes, see the Single Field Indexes guide.
Compound Index
The following code shows how to create a descending compound index:
auto index_specification = make_document(kvp("<field name 1>", -1), kvp("<field name 2>", -1)); auto result = collection.create_index(index_specification.view()); std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
Index created: { "name" : "<field name 1>_-1_<field name 2>_-1" }
To learn more about compound indexes, see the Compound Indexes guide.
Remove an Index
The following code shows how to remove an index:
collection.indexes().drop_one("<index name>"); std::cout << "Index dropped." << std::endl;
Index dropped.
To learn more about removing indexes, see the Remove an Index section of the Work With Indexes guide.
Remove All Indexes
The following code shows how to remove all indexes in a collection:
collection.indexes().drop_all(); std::cout << "All indexes removed." << std::endl;
All indexes removed.
To learn more about removing indexes, see the Remove an Index section of the Work With Indexes guide.