Loading...
Searching...
No Matches
Instance

How to use a MongoDB C++ Driver instance.

# Initialize the C++ Driver

## Basic Usage

void example() {
// Do not use mongocxx library interfaces at this point!
{
// Initialize the MongoDB C++ Driver by constructing the instance object.
mongocxx::instance instance;
// Use mongocxx library interfaces at this point.
use(mongocxx::client{});
// Cleanup the MongoDB C++ Driver by destroying the instance object.
}
// Do not use mongocxx library interfaces at this point!
}

# Errors

## Instance Recreation

void example() {
{
mongocxx::instance instance;
try {
mongocxx::instance another_instance; // Throws.
EXPECT(false && "should not reach this point");
} catch (mongocxx::exception const& ex) {
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
}
}

## Destroyed Instance

void example() {
// Initialize and cleanup.
{
mongocxx::instance instance;
}
try {
mongocxx::instance instance; // Throws.
EXPECT(false && "should not reach this point");
} catch (mongocxx::exception const& ex) {
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
try {
mongocxx::instance instance; // Throws.
EXPECT(false && "should not reach this point");
} catch (mongocxx::exception const& ex) {
EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance);
}
}