Rakesh Vidyadharan Help

Drop Index

Drop the specified index from the database:collection. The document represents the index specification. Additional options for the index (such as write concern) can be specified via the optional options sub-document.

One of the following properties must be specified in the document:

  • name - The name of the index to drop. Should be a string value.

  • specification - The full document specification of the index that was created.

Data models that represents the payload to be submitted to the service for retrieving aggregated information.

// // Created by Rakesh on 14/12/2024. // #pragma once #include "../../options/index.hpp" #include "action.hpp" #include <bsoncxx/builder/stream/document.hpp> namespace spt::mongoservice::api::model::request { template <util::Visitable Document> struct DropIndex { struct Specification { explicit Specification( Document&& document ) : specification{ std::forward<Document>( document ) } {} explicit Specification( bsoncxx::document::view bson ) { util::unmarshall( *this, bson ); } Specification() = default; ~Specification() = default; Specification(Specification&&) = default; Specification& operator=(Specification&&) = default; bool operator==(const Specification&) const = default; Specification(const Specification&) = delete; Specification& operator=(const Specification&) = delete; BEGIN_VISITABLES(Specification); VISITABLE(std::string, name); VISITABLE(std::optional<Document>, specification); END_VISITABLES; }; explicit DropIndex( Document&& document ) : document{ std::forward<Document>( document ) } {} DropIndex() = default; ~DropIndex() = default; DropIndex(DropIndex&&) = default; DropIndex& operator=(DropIndex&&) = default; DropIndex(const DropIndex&) = delete; DropIndex& operator=(const DropIndex&) = delete; BEGIN_VISITABLES(DropIndex); VISITABLE(std::optional<Specification>, document); VISITABLE(std::optional<options::Index>, options); std::string database; std::string collection; std::string application; std::string correlationId; Action action{Action::dropIndex}; bool skipMetric{false}; END_VISITABLES; }; }

Data models that represents the payloads the service responds with when returning matching aggregated information.

// // Created by Rakesh on 18/12/2024. // #pragma once #if defined __has_include #if __has_include("../../../common/visit_struct/visit_struct_intrusive.hpp") #include "../../../common/visit_struct/visit_struct_intrusive.hpp" #include "../../../common/util/serialise.hpp" #else #include <mongo-service/common/visit_struct/visit_struct_intrusive.hpp> #include <mongo-service/common/util/serialise.hpp> #endif #endif #include <string> namespace spt::mongoservice::api::model::response { struct DropIndex { explicit DropIndex( bsoncxx::document::view document ) { util::unmarshall( *this, document ); } DropIndex() = default; ~DropIndex() = default; DropIndex(DropIndex&&) = default; DropIndex& operator=(DropIndex&&) = default; DropIndex(const DropIndex&) = delete; DropIndex& operator=(const DropIndex&) = delete; BEGIN_VISITABLES(DropIndex); VISITABLE_DIRECT_INIT(bool, dropIndex, {false}); END_VISITABLES; }; }

Sample code illustrating the index action.

#include <mongo-service/api/repository/repository.hpp> #include <log/NanoLog.hpp> int main() { using namespace spt::mongoservice::api; auto index = model::request::DropIndex<bsoncxx::document::value>{ document{} << "str" << -1 << finalize }; index.database = "test"; index.collection = "test"; index.options = options::Index{}; index.options->collation = options::Collation{}; index.options->collation->locale = "en"; index.options->collation->strength = 1; index.options->name = "statusidx"; index.options->defaultLanguage = "en"; index.options->languageOverride = "en-gb"; index.options->expireAfterSeconds = std::chrono::seconds{ 1000 }; index.options->background = true; index.options->unique = false; index.options->hidden = false; index.options->sparse = false; auto result = repository::dropIndex( index ); if ( !result.has_value() ) { LOG_WARN << "Error dropping index. " << magic_enum::enum_name( result.error().cause ) << ". " << result.error().message; } }
Last modified: 18 February 2025