MongoDB C++ Driver mongocxx-4.0.0
Loading...
Searching...
No Matches
Databases

How to obtain and use databases.

Obtain a Database

void example(mongocxx::client client) {
mongocxx::database db = client["db"];
EXPECT(db);
EXPECT(db.name() == "db");
EXPECT(client.database("db").name() == "db");
}

Database Operations

Drop a Database

void example(mongocxx::client& client) {
mongocxx::database db = client["db"];
{
std::vector<std::string> names = client.list_database_names();
EXPECT(std::count(names.begin(), names.end(), "db") == 1);
}
db.drop();
{
std::vector<std::string> names = client.list_database_names();
EXPECT(std::count(names.begin(), names.end(), "db") == 0);
}
}

Run a Command

void example(mongocxx::database db) {
auto cmd = bsoncxx::from_json(R"({"ping": 1})");
bsoncxx::document::value reply = db.run_command(cmd.view());
EXPECT(reply["ok"]);
EXPECT(reply["ok"].get_double().value == 1.0);
}

Set a Read Concern

void example(mongocxx::database db) {
using rc_level = mongocxx::read_concern::level;
// Default.
{
mongocxx::read_concern rc = db.read_concern();
EXPECT(rc.acknowledge_level() == rc_level::k_server_default);
}
// Explicit.
{
rc.acknowledge_level(rc_level::k_majority);
// ... other read concern options.
db.read_concern(rc);
EXPECT(db.read_concern() == rc);
EXPECT(db.read_concern().acknowledge_level() == rc_level::k_majority);
}
}

Set a Write Concern

void example(mongocxx::database db) {
using wc_level = mongocxx::write_concern::level;
// Default.
{
mongocxx::write_concern wc = db.write_concern();
EXPECT(wc.acknowledge_level() == wc_level::k_default);
EXPECT(wc.timeout() == std::chrono::milliseconds(0));
}
// Explicit.
{
wc.majority(std::chrono::milliseconds(5000));
// ... other write concern options.
db.write_concern(wc);
EXPECT(db.write_concern() == wc);
EXPECT(db.write_concern().acknowledge_level() == wc_level::k_majority);
EXPECT(db.write_concern().timeout() == std::chrono::seconds(5));
}
}

Collection Operations

Create a Collection

void example(mongocxx::database db) {
EXPECT(!db.has_collection("coll"));
mongocxx::collection coll = db.create_collection("coll");
EXPECT(coll);
EXPECT(db.has_collection("coll"));
}

Create a Collection With Options

void example(mongocxx::database db) {
EXPECT(!db.has_collection("coll"));
auto opts = bsoncxx::from_json(R"({"validationLevel": "strict", "validationAction": "error"})");
// ... other create options.
mongocxx::collection coll = db.create_collection("coll", opts.view());
EXPECT(coll);
EXPECT(db.has_collection("coll"));
}

Query a Collection Exists

void example(mongocxx::database db) {
EXPECT(db.has_collection("present"));
EXPECT(!db.has_collection("missing"));
}

List Collections in the Database

void example(mongocxx::database db) {
int a = 0;
int b = 0;
int c = 0;
auto count_fields = [&a, &b, &c](mongocxx::cursor cursor) {
a = 0;
b = 0;
c = 0;
for (bsoncxx::document::view doc : cursor) {
EXPECT(doc["name"]);
auto name = bsoncxx::string::to_string(doc["name"].get_string().value);
if (name == "a") {
++a;
} else if (name == "b") {
++b;
} else if (name == "c") {
++c;
}
}
};
// Basic usage.
{
count_fields(db.list_collections());
EXPECT(a == 1); // Present.
EXPECT(b == 1); // Present.
EXPECT(c == 0); // Missing.
}
// With a filter.
{
auto filter = bsoncxx::from_json(R"({"name": {"$ne": "b"}})");
count_fields(db.list_collections(filter.view()));
EXPECT(a == 1); // Present.
EXPECT(b == 0); // Filtered.
EXPECT(c == 0); // Missing.
}
}

List Collection Names in the Database

void example(mongocxx::database db) {
// Basic usage.
{
std::vector<std::string> names = db.list_collection_names();
EXPECT(std::count(names.begin(), names.end(), "a") == 1); // Present.
EXPECT(std::count(names.begin(), names.end(), "b") == 1); // Present.
EXPECT(std::count(names.begin(), names.end(), "c") == 0); // Missing.
}
// With a filter.
{
auto filter = bsoncxx::from_json(R"({"name": {"$ne": "b"}})");
std::vector<std::string> names = db.list_collection_names(filter.view());
EXPECT(std::count(names.begin(), names.end(), "a") == 1); // Present.
EXPECT(std::count(names.begin(), names.end(), "b") == 0); // Filtered.
EXPECT(std::count(names.begin(), names.end(), "c") == 0); // Missing.
}
}

Error Handling

Invalid Database

void example() {
try {
auto name = db.name(); // DO NOT DO THIS. Throws.
EXPECT(false && "should not reach this point");
} catch (const mongocxx::exception& ex) {
EXPECT(ex.code() == mongocxx::error_code::k_invalid_database_object);
}
}