Rakesh Vidyadharan Help

Rename Collection

Rename a collection in the specified database. If a collection already exists with the document.target name, an error is returned. The option to automatically drop a pre-existing collection as supported by MongoDB is not supported. For such cases, use the Drop Collection action prior to invoking this action. Specify the write concern settings in the optional options sub-document.

This is a potentially heavy-weight operation. All version history documents for the specified database::collection combination are also updated. Version history document update is performed asynchronously. The operation enqueues an update operation to the version history documents, and returns. This can lead to queries against version history returning stale information for a short period of time.

Note: Renaming the collection in all associated version history documents may be the wrong choice. In chronological terms, those documents were associated with the previous collection. Only future revisions are associated with the renamed target. However, this can create issues in terms of retrieval, or if iterating over records for some other purpose, or if a new collection with the previous name is created in future.

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/dropcollection.hpp" #include "action.hpp" namespace spt::mongoservice::api::model::request { struct RenameCollection { struct Document { explicit Document( std::string target ) : target{ std::move( target ) } {} Document() = default; ~Document() = default; Document(Document&&) = default; Document& operator=(Document&&) = default; bool operator==(const Document&) const = default; Document(const Document&) = delete; Document& operator=(const Document&) = delete; BEGIN_VISITABLES(Document); VISITABLE(std::string, target); END_VISITABLES; }; explicit RenameCollection( std::string target ) : document{ std::move( target ) } {} RenameCollection() = default; ~RenameCollection() = default; RenameCollection(RenameCollection&&) = default; RenameCollection& operator=(RenameCollection&&) = default; RenameCollection(const RenameCollection&) = delete; RenameCollection& operator=(const RenameCollection&) = delete; BEGIN_VISITABLES(RenameCollection); VISITABLE(Document, document); VISITABLE(std::optional<options::DropCollection>, options); VISITABLE(std::string, database); VISITABLE(std::string, collection); VISITABLE(std::string, application); VISITABLE(std::string, correlationId); VISITABLE_DIRECT_INIT(Action, action, {Action::renameCollection}); 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 rename = model::request::RenameCollection{ "renamed" }; rename.database = "test"; rename.collection = "test"; auto result = repository::collection( rename ); if ( !result.has_value() ) { LOG_WARN << "Error renaming collection. " << magic_enum::enum_name( result.error().cause ) << ". " << result.error().message; } }
Last modified: 18 February 2025