Delete
The document
represents the query to execute to find the candidate documents to delete from the database
: collection
. The query is executed to retrieve the candidate documents, and the documents removed from the specified database:collection
. The retrieved documents are then written to the version history database.
Data models that represents the payload to be submitted to the service for deleting document(s).
//
// Created by Rakesh on 13/12/2024.
//
#pragma once
#include "../../options/delete.hpp"
#include "action.hpp"
#include <bsoncxx/builder/stream/document.hpp>
namespace spt::mongoservice::api::model::request
{
template <util::Visitable Document, util::Visitable Metadata>
requires std::constructible_from<Metadata, bsoncxx::document::view>
struct Delete
{
explicit Delete( Document&& document ) : document{ std::forward<Document>( document ) } {}
Delete() = default;
~Delete() = default;
Delete(Delete&&) = default;
Delete& operator=(Delete&&) = default;
Delete(const Delete&) = delete;
Delete& operator=(const Delete&) = delete;
BEGIN_VISITABLES(Delete);
VISITABLE(std::optional<Document>, document);
VISITABLE(std::optional<options::Delete>, options);
std::optional<Metadata> metadata;
std::string database;
std::string collection;
std::string application;
std::string correlationId;
Action action{Action::_delete};
bool skipMetric{false};
END_VISITABLES;
};
}
Data models that represents the payloads the service responds with when deleting document(s).
//
// Created by Rakesh on 17/12/2024.
//
#pragma once
#include "history.hpp"
#include <vector>
namespace spt::mongoservice::api::model::response
{
struct Delete
{
explicit Delete( bsoncxx::document::view document ) { util::unmarshall( *this, document ); }
Delete() = default;
~Delete() = default;
Delete(Delete&&) = default;
Delete& operator=(Delete&&) = default;
Delete(const Delete&) = delete;
Delete& operator=(const Delete&) = delete;
BEGIN_VISITABLES(Delete);
VISITABLE(std::vector<bsoncxx::oid>, success);
VISITABLE(std::vector<bsoncxx::oid>, failure);
VISITABLE(std::vector<History>, history);
END_VISITABLES;
};
}
Sample code illustrating the delete action.
#include <mongo-service/api/repository/repository.hpp>
#include <log/NanoLog.hpp>
namespace example
{
struct Metadata
{
explicit Metadata( bsoncxx::document::view bson ) { spt::util::unmarshall( *this, bson ); }
Metadata() = default;
~Metadata() = default;
Metadata(Metadata&&) = default;
Metadata& operator=(Metadata&&) = default;
bool operator==(const Metadata&) const = default;
Metadata(const Metadata&) = delete;
Metadata& operator=(const Metadata&) = delete;
BEGIN_VISITABLES(Metadata);
VISITABLE(std::string, project);
VISITABLE(std::string, product);
END_VISITABLES;
};
}
int main()
{
using namespace spt::mongoservice::api;
auto remove = model::request::Delete<model::request::IdFilter, example::Metadata>{ model::request::IdFilter{ oid } };
remove.database = "test";
remove.collection = "test";
auto result = repository::remove( remove );
if ( !result.has_value() )
{
LOG_WARN << "Error deleting document. " << magic_enum::enum_name( result.error().cause ) << ". " << result.error().message;
}
}
Last modified: 18 February 2025