How to obtain and use collections.
Obtain a Collection
EXPECT(coll);
EXPECT(coll.
name() ==
"coll");
}
Collection Operations
Drop a Collection
Rename a Collection
EXPECT(coll.
name() ==
"old");
EXPECT(coll.
name() ==
"new");
}
Set a Read Concern
{
}
{
EXPECT(coll.
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(coll.
write_concern().acknowledge_level() == wc_level::k_majority);
EXPECT(coll.
write_concern().timeout() == std::chrono::seconds(5));
}
}
Set a Read Preference
{
EXPECT(coll.
read_preference().mode() == mongocxx::read_preference::read_mode::k_primary);
}
{
rp.
mode(mongocxx::read_preference::read_mode::k_secondary);
}
}
Index Operations
On a Collection
List Indexes
EXPECT(doc["name"]);
EXPECT(doc[
"name"].
type() == bsoncxx::type::k_string);
EXPECT(doc["key"]);
EXPECT(doc[
"key"].
type() == bsoncxx::type::k_document);
}
}
Create an Index
EXPECT(result["name"]);
EXPECT(result["name"].get_string().value == "key_1");
}
Create an Index With Options
{
EXPECT(result["name"]);
EXPECT(result["name"].get_string().value == "custom_name");
}
{
EXPECT(result["name"]);
EXPECT(result["name"].get_string().value == "key_b_1");
}
}
With an Index View
Obtain an Index View
EXPECT(doc["key"]);
EXPECT(doc["name"]);
}
}
List Indexes
EXPECT(doc["name"]);
EXPECT(doc[
"name"].
type() == bsoncxx::type::k_string);
EXPECT(doc["key"]);
EXPECT(doc[
"key"].
type() == bsoncxx::type::k_document);
}
}
Create an Index
{
EXPECT(result_opt);
EXPECT(result_opt->compare("x_1") == 0);
}
{
EXPECT(result_opt);
EXPECT(result_opt->compare("y_1") == 0);
}
}
Create an Index With Options
{
EXPECT(result_opt);
EXPECT(result_opt->compare("one") == 0);
}
{
});
EXPECT(result_opt);
EXPECT(result_opt->compare("two") == 0);
}
{
EXPECT(result_opt);
EXPECT(result_opt->compare("z_1") == 0);
}
}
Create Multiple Indexes
std::vector<mongocxx::index_model> models;
if (result["raw"]) {
result = result["raw"].get_document().value.begin()->get_document().value;
}
EXPECT(result["ok"]);
EXPECT(result["ok"].get_double().value == 1.0);
EXPECT(result["numIndexesBefore"]);
EXPECT(result["numIndexesBefore"].get_int32().value == 1);
EXPECT(result["numIndexesAfter"]);
EXPECT(result["numIndexesAfter"].get_int32().value == 3);
}
Drop an Index
auto count_indexes = [&indexes] {
auto cursor = indexes.
list();
return std::distance(cursor.begin(), cursor.end());
};
EXPECT(count_indexes() == 3);
EXPECT(count_indexes() == 2);
EXPECT(count_indexes() == 1);
}
Drop All Indexes
auto count_indexes = [&indexes] {
auto cursor = indexes.
list();
return std::distance(cursor.begin(), cursor.end());
};
EXPECT(count_indexes() == 3);
EXPECT(count_indexes() == 1);
}
Document Operations
Query the Number of Documents
Estimate the Number of Documents
Find a Document
{
auto result_opt = coll.
find_one(filter.view());
EXPECT(result_opt);
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
}
{
EXPECT(result_opt);
}
}
Find Multiple Documents
{
int count = 0;
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value != 2);
++count;
}
EXPECT(count == 2);
}
{
std::vector<bsoncxx::document::value> docs{cursor.
begin(), cursor.
end()};
EXPECT(docs.size() == 3u);
}
}
Delete a Document
{
EXPECT(result_opt);
}
{
}
}
Delete Many Documents
{
EXPECT(result_opt);
}
{
}
}
Insert a Document
{
EXPECT(result_opt);
}
{
}
}
Insert Many Documents
{
std::vector<bsoncxx::document::view> docs = {x1.view(), x2.view()};
EXPECT(result_opt);
}
{
auto result_opt = coll.
insert_many(std::begin(docs), std::end(docs));
EXPECT(result_opt);
}
{
std::array<bsoncxx::document::view, 2> docs = {z1.view(), z2.view()};
}
}
Replace a Document
{
auto result_opt = coll.
replace_one(filter.view(), x0.view());
EXPECT(result_opt);
}
{
EXPECT(coll.replace_one(filter.view(), x0.view(), opts));
EXPECT(coll.replace_one(filter.view(), x0.view(), opts));
EXPECT(coll.replace_one(filter.view(), x0.view(), opts));
}
}
Update a Document
{
auto result_opt = coll.
update_one(filter.view(), update.view());
EXPECT(result_opt);
}
{
}
}
Update Multiple Documents
{
auto result_opt = coll.
update_many(filter.view(), update.view());
EXPECT(result_opt);
}
{
EXPECT(coll.
update_many(original.view(), update.view(), opts));
EXPECT(coll.
update_many(original.view(), update.view(), opts));
}
}
Find and Delete a Document
{
EXPECT(result_opt);
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
}
{
EXPECT(result_opt);
EXPECT(*result_opt == x3);
}
}
Find and Replace a Document
{
EXPECT(result_opt);
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
}
{
EXPECT(result_opt);
EXPECT(*result_opt == x3);
}
}
Find and Update a Document
{
EXPECT(result_opt);
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
}
{
EXPECT(result_opt);
EXPECT(*result_opt == x3);
}
}
Find Distinct Values
{
std::vector<bsoncxx::document::value> docs{cursor.begin(), cursor.end()};
EXPECT(docs.size() == 1u);
auto doc = docs[0].view();
EXPECT(doc["values"]);
EXPECT(doc[
"values"].
type() == bsoncxx::type::k_array);
auto arr = doc["values"].get_array().value;
EXPECT(std::distance(arr.begin(), arr.end()) == 2);
EXPECT(arr[0].get_int32().value == 1);
EXPECT(arr[1].get_int32().value == 2);
}
{
EXPECT(std::distance(cursor.begin(), cursor.end()) == 1);
}
}
Execute a Single Bulk Write Operation
{
auto result_opt = coll.
write(insert);
EXPECT(result_opt);
EXPECT(result_opt->inserted_count() == 1);
}
{
}
}
Execute Multiple Bulk Write Operations
auto result_opt = bulk_write.
execute();
EXPECT(result_opt);
}
Execute an Aggregation Operation
{
EXPECT(std::count_if(cursor.
begin(), cursor.
end(), has_field_x) == 1);
}
{
EXPECT(std::distance(cursor.
begin(), cursor.
end()) == 2);
}
}
Error Handling
Invalid Collection
{
}
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}
Invalid Parameter
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}
Incompatible Options
{
}
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}