On this page, you can see copyable code examples that demonstrate how to use the C++ driver to work with common types of indexes.
TipTo 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.
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#include <iostream>23#include <bsoncxx/builder/basic/document.hpp>4#include <bsoncxx/json.hpp>5#include <mongocxx/client.hpp>6#include <mongocxx/exception/exception.hpp>7#include <mongocxx/instance.hpp>8#include <mongocxx/uri.hpp>910using bsoncxx::builder::basic::kvp;11using bsoncxx::builder::basic::make_document;1213int main() {14 try {15 mongocxx::instance instance;1617 mongocxx::uri uri("<connection string>");18 mongocxx::client client(uri);1920 auto database = client["<database name>"];21 auto collection = database["<collection name>"];2223 2425 2627 } 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}
The following code shows how to create an ascending single-field index:
auto index_specification = make_document(kvp("<fieldName>", 1));auto result = collection.create_index(index_specification.view());std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
Index created: { "name" : "fieldName_1" }
To learn more about single-field indexes, see the Single Field Indexes guide.
The following code shows how to create a descending compound index:
auto index_specification = make_document(kvp("<fieldName1>", -1), kvp("<fieldName2>", -1));auto result = collection.create_index(index_specification.view());std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
Index created: { "name" : "fieldName1_-1_fieldName2_-1" }
To learn more about compound indexes, see the Compound Indexes guide.
The following code shows how to remove an index:
collection.indexes().drop_one("<indexName>");std::cout << "Index dropped." << std::endl;
To learn more about removing indexes, see the Remove an Index section of the Work With Indexes guide.
The following code shows how to remove all indexes in a collection:
collection.indexes().drop_all();std::cout << "All indexes removed." << std::endl;
To learn more about removing indexes, see the Remove an Index section of the Work With Indexes guide.
The following sections contain code examples that describe how to manage Atlas Search indexes. To learn more about Atlas Search indexes, see the Atlas Search Indexes guide.
The following code shows how to create an Atlas Search index that dynamically indexes all supported fields in the specified collection:
auto siv = collection.search_indexes();auto name = "<searchIndexName>";auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true))));auto model = mongocxx::search_index_model(name, definition.view());auto result = siv.create_one(model);std::cout << "New index name: " << result << std::endl;
New index name: searchIndexName
The following code prints a list of Atlas Search indexes in the specified collection:
auto siv = collection.search_indexes();auto result = siv.list(); for (const auto &idx : result) { std::cout << bsoncxx::to_json(idx) << std::endl;}
The following code updates an existing Atlas Search index with the specified new index definition:
auto siv = collection.search_indexes();auto update_fields = make_document(kvp("<fieldName>", make_document(kvp("type", "<fieldType>"))));auto update_definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", update_fields))));siv.update_one("<searchIndexName>", update_definition.view());
The following code deletes an Atlas Search index with the specified name:
auto siv = collection.search_indexes();siv.drop_one("<searchIndexName>");
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4