
MongoDBDriverServer::executeWriteCommand 메서드의 파라미터 인 CommandDocument는 MongoDB의 명령을 나타내는 문서입니다. CommandDocument는 다음 구조를 가지고 있습니다.
- 명령 이름 (ex: insert, update, delete)
- 명령에 필요한 파라미터 (ex: 데이터베이스 이름, 컬렉션 이름, 업데이트 내용 등)
예를 들어, 데이터베이스 이름이 "mydb"이고 컬렉션 이름이 "mycoll"인 데이터를 삽입하는 명령을 생성하려면 다음과 같이 CommandDocument를 생성할 수 있습니다.
#hostingforum.kr
cpp
#include
#include
#include
#include
int main() {
// MongoDB 클라이언트 인스턴스 생성
mongocxx::instance inst;
mongocxx::client conn{mongocxx::uri{}};
// CommandDocument 생성
bsoncxx::builder::stream::document doc{};
doc << "insert" << "mydb.mycoll" << bsoncxx::builder::stream::open_document()
<< "mykey" << "myvalue" << bsoncxx::builder::stream::close_document();
bsoncxx::document::value cmd_doc = doc.view();
// MongoDB에 명령 보내기
conn["mydb"]["mycoll"].run_command(cmd_doc).wait();
return 0;
}
위 예제에서 `run_command` 메서드는 `executeWriteCommand` 메서드와 유사하게 MongoDB에 명령을 보냅니다. `executeWriteCommand` 메서드는 `run_command` 메서드와 동일한 방식으로 동작합니다.
2025-03-11 01:33