Rakesh Vidyadharan Help

Count

Protocol for counting documents that match a filter.

Data model that represents the payload to be submitted to the service for retrieving documents. Model supports raw BSON document queries as well as structures that can be serialised to BSON.

// // Created by Rakesh on 13/12/2024. // #pragma once #include "../../options/count.hpp" #include "action.hpp" #include <bsoncxx/builder/stream/document.hpp> namespace spt::mongoservice::api::model::request { template <util::Visitable Document> struct Count { explicit Count( Document&& document ) : document{ std::forward<Document>( document ) } {} Count() = default; ~Count() = default; Count(Count&&) = default; Count& operator=(Count&&) = default; Count(const Count&) = delete; Count& operator=(const Count&) = delete; BEGIN_VISITABLES(Count); VISITABLE(std::optional<Document>, document); VISITABLE(std::optional<options::Count>, options); std::string database; std::string collection; std::string application; std::string correlationId; Action action{Action::count}; bool skipMetric{false}; END_VISITABLES; }; }

Data model that represents the payload that the service responds with when returning count of documents.

// // Countd 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 #include <cstdint> #include <optional> #include <string> namespace spt::mongoservice::api::model::response { struct Count { explicit Count( bsoncxx::document::view document ) { util::unmarshall( *this, document ); } Count() = default; ~Count() = default; Count(Count&&) = default; Count& operator=(Count&&) = default; Count(const Count&) = delete; Count& operator=(const Count&) = delete; BEGIN_VISITABLES(Count); VISITABLE_DIRECT_INIT(int64_t, count, {0}); END_VISITABLES; }; }

Sample code illustrating the count action.

#include <mongo-service/api/repository/repository.hpp> #include <log/NanoLog.hpp> int main() { using namespace spt::mongoservice::api; auto count = model::request::Count<bsoncxx::document::value>{ document{} << finalize }; count.database = "test"; count.collection = "test"; count.options.emplace(); count.options->maxTime = std::chrono::milliseconds{ 500 }; count.options->limit = 100'000; auto result = repository::count( count ); if ( !result.has_value() ) { LOG_WARN << "Error counting documents. " << magic_enum::enum_name( result.error().cause ) << ". " << result.error().message; } }
Last modified: 18 February 2025