How to use clients and client pools.
Create a Client
Single
Basic Usage
void example() {
{
EXPECT(!client);
}
{
EXPECT(client);
EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri);
}
}
With a Custom URI
void example() {
EXPECT(client);
EXPECT(client.uri().to_string() == uri.to_string());
}
With Client Options
Stable API Options
mongocxx::options::server_api::version::k_version_1};
server_api_opts.strict(true);
client_opts.server_api_opts(server_api_opts);
EXPECT(client);
EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri);
return client;
}
TLS Options
client_opts.tls_opts(tls_opts);
mongocxx::uri uri{
"mongodb://bob:pwd123@localhost:27017/?tls=true"};
EXPECT(client);
EXPECT(client.uri().to_string() == uri.to_string());
return client;
}
Automatic Encryption Options
auto_encryption_opts.key_vault_namespace({"keyvault", "datakeys"});
auto_encryption_opts.kms_providers(kms_providers);
R"({"mongocryptdURI": "mongodb://localhost:27027", "mongocryptdSpawnArgs": ["--port", "27027"]})"));
client_opts.auto_encryption_opts(auto_encryption_opts);
EXPECT(client);
}
APM Options
void example() {
apm_opts.on_command_started(&on_command_started_callback);
client_opts.apm_opts(apm_opts);
EXPECT(client);
}
Pool
Basic Usage
void example() {
{
mongocxx::pool::entry entry = pool.acquire();
EXPECT(entry);
EXPECT(client);
EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri);
}
{
mongocxx::pool::entry entry = pool.acquire();
EXPECT(entry);
EXPECT(client);
EXPECT(client.uri().to_string() == uri.to_string());
}
}
With Client Options
void example() {
mongocxx::pool::entry entry = pool.acquire();
EXPECT(entry);
EXPECT(client);
EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri);
}
Try Acquire
void example() {
mongocxx::uri uri{
"mongodb://localhost:27017/?maxPoolSize=1&waitQueueTimeoutMS=1"};
auto entry_opt = pool.try_acquire();
EXPECT(entry_opt);
EXPECT(*entry_opt);
{
mongocxx::pool::entry hold = std::move(*entry_opt);
EXPECT(hold);
entry_opt = pool.try_acquire();
EXPECT(!entry_opt);
}
entry_opt = pool.try_acquire();
EXPECT(entry_opt);
EXPECT(*entry_opt);
}
Use a Client
List Databases
int count = 0;
EXPECT(doc["name"]);
EXPECT(doc["sizeOnDisk"]);
EXPECT(doc["empty"]);
if (doc["name"].get_string().value == "admin") {
++count;
}
}
EXPECT(count == 1);
}
List Databases With Options
int count = 0;
EXPECT(doc["name"]);
EXPECT(!doc["sizeOnDisk"]);
EXPECT(!doc["empty"]);
if (doc["name"].get_string().value == "admin") {
++count;
}
}
EXPECT(count == 1);
}
List Database Names
std::vector<std::string> names = client.list_database_names();
EXPECT(std::count(names.begin(), names.end(), "admin") == 1);
}
List Database Names With Options
std::vector<std::string> names = client.list_database_names(opts.view());
EXPECT(std::count(names.begin(), names.end(), "admin") == 0);
}
Error Handling
Invalid Client
void example() {
EXPECT(!client);
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_client_object);
}
}
Wait Queue Timeout
void example() {
try {
mongocxx::uri{
"mongodb://localhost:27017/?maxPoolSize=1&waitQueueTimeoutMS=1"}};
mongocxx::pool::entry hold = pool.acquire();
EXPECT(hold);
mongocxx::pool::entry entry = pool.acquire();
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_pool_wait_queue_timeout);
}
}
Invalid Stable API Options
void example() {
try {
mongocxx::options::server_api::version version =
mongocxx::options::server_api::version_from_string("0");
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
try {
std::string version = mongocxx::options::server_api::version_to_string(
static_cast<mongocxx::options::server_api::version>(1));
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}
TLS Not Enabled
void example() {
try {
EXPECT(false && "should not reach this point");
EXPECT(
ex.code() == mongocxx::error_code::k_invalid_parameter ||
ex.code() == mongocxx::error_code::k_ssl_not_supported ||
false);
}
}
Invalid Auto Encryption Options
void example() {
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code().value() == 58);
}
try {
.key_vault_namespace({"keyvault", "datakeys"})
EXPECT(false && "should not reach this point");
EXPECT(ex.code().value() == 1);
}
try {
nullptr))};
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}