How to obtain and use databases.
Obtain a Database
EXPECT(db);
EXPECT(db.
name() ==
"db");
}
Database Operations
Drop a Database
{
EXPECT(std::count(names.begin(), names.end(), "db") == 1);
}
{
EXPECT(std::count(names.begin(), names.end(), "db") == 0);
}
}
Run a Command
EXPECT(reply["ok"]);
EXPECT(reply["ok"].get_double().value == 1.0);
}
Set a Read Concern
{
}
{
EXPECT(db.
read_concern().acknowledge_level() == rc_level::k_majority);
}
}
Set a Write Concern
{
EXPECT(wc.
timeout() == std::chrono::milliseconds(0));
}
{
wc.
majority(std::chrono::milliseconds(5000));
EXPECT(db.
write_concern().acknowledge_level() == wc_level::k_majority);
}
}
Collection Operations
Create a Collection
Create a Collection With Options
auto opts =
bsoncxx::from_json(R
"({"validationLevel": "strict", "validationAction": "error"})");
EXPECT(coll);
}
Query a Collection Exists
List Collections in the Database
int a = 0;
int b = 0;
int c = 0;
a = 0;
b = 0;
c = 0;
EXPECT(doc["name"]);
if (name == "a") {
++a;
} else if (name == "b") {
++b;
} else if (name == "c") {
++c;
}
}
};
{
EXPECT(a == 1);
EXPECT(b == 1);
EXPECT(c == 0);
}
{
EXPECT(a == 1);
EXPECT(b == 0);
EXPECT(c == 0);
}
}
List Collection Names in the Database
{
EXPECT(std::count(names.begin(), names.end(), "a") == 1);
EXPECT(std::count(names.begin(), names.end(), "b") == 1);
EXPECT(std::count(names.begin(), names.end(), "c") == 0);
}
{
EXPECT(std::count(names.begin(), names.end(), "a") == 1);
EXPECT(std::count(names.begin(), names.end(), "b") == 0);
EXPECT(std::count(names.begin(), names.end(), "c") == 0);
}
}
Error Handling
Invalid Database
void example() {
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_database_object);
}
}