Skip to content

Commit

Permalink
(DOCSP-40808) @kevinAlbs Review changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
elyse-mdb committed Nov 8, 2024
1 parent 026437a commit 8a66ab3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 61 deletions.
4 changes: 1 addition & 3 deletions source/includes/indexes/indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ int main(){
std::cout << bsoncxx::to_json(*it) << std::endl;
}
// end-list-search-index
}
// Print list() output using a range-based for loop
{
// Print list() output using a range-based for loop
for (const auto &idx : cursor) {
std::cout << bsoncxx::to_json(idx) << std::endl;
}
Expand Down
149 changes: 91 additions & 58 deletions source/includes/usage-examples/index-code-examples.cpp
Original file line number Diff line number Diff line change
@@ -1,58 +1,91 @@
// start-single-field
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;
// end-single-field

// start-compound-field
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;
// end-compound-field

// start-remove-index
collection.indexes().drop_one("<indexName>");

std::cout << "Index dropped." << std::endl;
// end-remove-index

// start-remove-all-indexes
collection.indexes().drop_all();

std::cout << "All indexes removed." << std::endl;
// end-remove-all-indexes

// start-create-search-index
// Create an index model with your index name and definition
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());

// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << std::endl;
// end-create-search-index


// start-list-search-indexes
auto siv = collection.search_indexes();
auto result = siv.list();
for (const auto &idx : result) {
std::cout << bsoncxx::to_json(idx) << std::endl;
}
// end-list-search-indexes

// start-update-search-index
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());
// end-update-search-index

// start-remove-search-index
auto siv = collection.search_indexes();
siv.drop_one("<searchIndexName>");
// end-remove-search-index
/*
To build:
brew install mongo-cxx-driver
clang++ -o indexes main.cpp -std=c++17 $(pkg-config --libs --cflags libmongocxx)
*/
#include <iostream>

#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;

int main() {

mongocxx::instance instance;
mongocxx::uri uri("<connectionString>");
mongocxx::client client(uri);

auto db = client["<databaseName>"];
auto collection = db["<collectionName>"];

{
// start-single-field
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;
// end-single-field
}
{
// start-compound-field
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;
// end-compound-field
}
{
// start-remove-index
collection.indexes().drop_one("<indexName>");

std::cout << "Index dropped." << std::endl;
// end-remove-index

// start-remove-all-indexes
collection.indexes().drop_all();

std::cout << "All indexes removed." << std::endl;
// end-remove-all-indexes
}
{
// start-create-search-index
// Create an index model with your index name and definition
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());

// Create the search index
auto result = siv.create_one(model);
std::cout << "New index name: " << result << std::endl;
// end-create-search-index
}
{
// start-list-search-indexes
auto siv = collection.search_indexes();
auto result = siv.list();
for (const auto &idx : result) {
std::cout << bsoncxx::to_json(idx) << std::endl;
}
// end-list-search-indexes
}
{
// start-update-search-index
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());
// end-update-search-index
}
{
// start-remove-search-index
auto siv = collection.search_indexes();
siv.drop_one("<searchIndexName>");
// end-remove-search-index
}
}

0 comments on commit 8a66ab3

Please sign in to comment.