Create Index
Ensure appropriate index exists in the specified database:collection
. The document
represents the specification for the index to be created. Additional options for the index (such as unique) can be specified via the optional options
sub-document.
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 Index
{
explicit Index( Document&& document ) : document{ std::forward<Document>( document ) } {}
Index() = default;
~Index() = default;
Index(Index&&) = default;
Index& operator=(Index&&) = default;
Index(const Index&) = delete;
Index& operator=(const Index&) = delete;
BEGIN_VISITABLES(Index);
VISITABLE(std::optional<Document>, document);
VISITABLE(std::optional<options::Index>, options);
std::string database;
std::string collection;
std::string application;
std::string correlationId;
Action action{Action::index};
bool skipMetric{false};
END_VISITABLES;
};
}
Data models that represents the payloads the service responds with when returning matching aggregated information.
//
// Retrieved by Rakesh on 17/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
namespace spt::mongoservice::api::model::response
{
template <util::Visitable Document>
requires std::is_same_v<decltype(Document::id), bsoncxx::oid>
struct Retrieve
{
explicit Retrieve( bsoncxx::document::view document ) { util::unmarshall( *this, document ); }
Retrieve() = default;
~Retrieve() = default;
Retrieve(Retrieve&&) = default;
Retrieve& operator=(Retrieve&&) = default;
Retrieve(const Retrieve&) = delete;
Retrieve& operator=(const Retrieve&) = delete;
BEGIN_VISITABLES(Retrieve);
VISITABLE(std::optional<Document>, result);
VISITABLE(std::vector<Document>, results);
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::Index<bsoncxx::document::value>{ document{} << "str" << -1 << finalize };
index.database = "test";
index.collection = "test";
index.options.emplace();
index.options->collation.emplace();
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::duration_cast<std::chrono::seconds>( std::chrono::days{ 365 } );
index.options->background = true;
index.options->unique = false;
index.options->hidden = false;
index.options->sparse = true;
auto result = repository::index( index );
if ( !result.has_value() )
{
LOG_WARN << "Error ensuring index. " << magic_enum::enum_name( result.error().cause ) << ". " << result.error().message;
}
}
Last modified: 18 February 2025