Rakesh Vidyadharan Help

Create Collection

Create a collection in the specified database. If a collection already exists in the database with the same name, an error is returned. This is primarily useful when clients wish to specify additional options when creating a collection (eg. create a timeseries collection).

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

// // Created by Rakesh on 17/12/2024. // #pragma once #include "../../options/createcollection.hpp" #include "action.hpp" namespace spt::mongoservice::api::model::request { struct CreateCollection { explicit CreateCollection( options::CreateCollection document ) : document{ std::move( document ) } {} CreateCollection() = default; ~CreateCollection() = default; CreateCollection(CreateCollection&&) = default; CreateCollection& operator=(CreateCollection&&) = default; CreateCollection(const CreateCollection&) = delete; CreateCollection& operator=(const CreateCollection&) = delete; BEGIN_VISITABLES(CreateCollection); VISITABLE(std::optional<options::CreateCollection>, document); VISITABLE(std::string, database); VISITABLE(std::string, collection); VISITABLE(std::string, application); VISITABLE(std::string, correlationId); VISITABLE_DIRECT_INIT(Action, action, {Action::createCollection}); VISITABLE_DIRECT_INIT(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 CreateCollection { explicit CreateCollection( bsoncxx::document::view document ) { util::unmarshall( *this, document ); } CreateCollection() = default; ~CreateCollection() = default; CreateCollection(CreateCollection&&) = default; CreateCollection& operator=(CreateCollection&&) = default; CreateCollection(const CreateCollection&) = delete; CreateCollection& operator=(const CreateCollection&) = delete; BEGIN_VISITABLES(CreateCollection); VISITABLE(std::string, database); VISITABLE(std::string, collection); 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 create = model::request::CreateCollection{ options::CreateCollection{} }; create.database = "test"; create.collection = "test"; create.document->timeseries.emplace(); create.document->timeseries->timeField = "timestamp"; create.document->timeseries->metaField = "tags"; create.document->timeseries->granularity = options::CreateCollection::Timeseries::Granularity::hours; create.document->expireAfterSeconds = std::chrono::seconds{ 365*24*60*60 }; auto result = repository::collection( create ); if ( !result.has_value() ) { LOG_WARN << "Error creating collection. " << magic_enum::enum_name( result.error().cause ) << ". " << result.error().message; } }
Last modified: 18 February 2025