testcontainers-cpp 0.2.0
Native C++20 Testcontainers over the Docker Engine API
Loading...
Searching...
No Matches
testcontainers::modules::MongoDBContainer Class Reference

A running MongoDB server (a single-node replica-set PRIMARY): connection getters plus the owned container. More...

#include <testcontainers/modules/MongoDB.hpp>

Public Member Functions

const std::string & host () const noexcept
 The address a client in the test process connects to.
std::uint16_t port () const noexcept
 The host port published for the server port 27017.
std::string connection_string () const
 A ready-to-use MongoDB URI for the configured default database: mongodb://<host>:<port>/<database>?directConnection=true.
std::string connection_string (const std::string &database) const
 Same URI pointing at database instead of the configured default.
const std::string & replica_set_name () const noexcept
 The replica-set name the server runs under (for rs.status()-style assertions or hand-built in-network URIs).
const std::string & database () const noexcept
 The configured default database name.
ExecResult mongosh (const std::string &js) const
 Run a JavaScript snippet inside the container via mongosh --quiet --eval against the local server and return its output and exit code — seeding and assertions without a C++ driver: mongo.mongosh("db.orders.countDocuments({})").stdout_data == "0\n".
Containercontainer () noexcept
 The underlying container handle: exec, read logs, copy files, stop() early, keep() it past the test.
const Containercontainer () const noexcept

Friends

class MongoDBImage

Detailed Description

A running MongoDB server (a single-node replica-set PRIMARY): connection getters plus the owned container.

Move-only — it owns the core Container, whose destructor force-removes the server (RAII teardown; container().keep() or reuse opt out, see Container).

Member Function Documentation

◆ host()

const std::string & testcontainers::modules::MongoDBContainer::host ( ) const
inlinenoexcept

The address a client in the test process connects to.

Resolved once, when the container started.

◆ port()

std::uint16_t testcontainers::modules::MongoDBContainer::port ( ) const
inlinenoexcept

The host port published for the server port 27017.

Resolved once, when the container started.

◆ connection_string() [1/2]

std::string testcontainers::modules::MongoDBContainer::connection_string ( ) const
inline

A ready-to-use MongoDB URI for the configured default database: mongodb://<host>:<port>/<database>?directConnection=true.

Feed it verbatim to mongocxx (or any driver / CLI tool).

directConnection=true makes every driver talk to this server directly instead of discovering the replica set — necessary because the set's advertised member address is container-internal and not reachable through the port mapping. Transactions and change streams work over a direct connection; do not remove the parameter, and do not add replicaSet= (it would switch drivers back into discovery).

◆ connection_string() [2/2]

std::string testcontainers::modules::MongoDBContainer::connection_string ( const std::string & database) const

Same URI pointing at database instead of the configured default.

An empty name yields mongodb://<host>:<port>/?directConnection=true (the driver default applies; the '/' stays — strict URI parsers reject options without it).

◆ replica_set_name()

const std::string & testcontainers::modules::MongoDBContainer::replica_set_name ( ) const
inlinenoexcept

The replica-set name the server runs under (for rs.status()-style assertions or hand-built in-network URIs).

◆ database()

const std::string & testcontainers::modules::MongoDBContainer::database ( ) const
inlinenoexcept

The configured default database name.

◆ mongosh()

ExecResult testcontainers::modules::MongoDBContainer::mongosh ( const std::string & js) const

Run a JavaScript snippet inside the container via mongosh --quiet --eval against the local server and return its output and exit code — seeding and assertions without a C++ driver: mongo.mongosh("db.orders.countDocuments({})").stdout_data == "0\n".

The snippet starts in the configured default database.

◆ container() [1/2]

Container & testcontainers::modules::MongoDBContainer::container ( )
inlinenoexcept

The underlying container handle: exec, read logs, copy files, stop() early, keep() it past the test.

◆ container() [2/2]

const Container & testcontainers::modules::MongoDBContainer::container ( ) const
inlinenoexcept

◆ MongoDBImage

friend class MongoDBImage
friend