How to obtain and use collections.
Obtain a Collection
EXPECT(coll);
EXPECT(coll.name() == "coll");
EXPECT(db.collection("coll").name() == "coll");
}
Collection Operations
Drop a Collection
EXPECT(db.has_collection("coll"));
coll.drop();
EXPECT(!db.has_collection("coll"));
}
Rename a Collection
EXPECT(db.has_collection("old"));
EXPECT(!db.has_collection("new"));
EXPECT(coll.name() == "old");
coll.rename("new");
EXPECT(coll.name() == "new");
EXPECT(!db.has_collection("old"));
EXPECT(db.has_collection("new"));
}
Set a Read Concern
using rc_level = mongocxx::read_concern::level;
{
EXPECT(rc.acknowledge_level() == rc_level::k_server_default);
}
{
rc.acknowledge_level(rc_level::k_majority);
coll.read_concern(rc);
EXPECT(coll.read_concern() == rc);
EXPECT(coll.read_concern().acknowledge_level() == rc_level::k_majority);
}
}
Set a Write Concern
using wc_level = mongocxx::write_concern::level;
{
EXPECT(wc.acknowledge_level() == wc_level::k_default);
EXPECT(wc.timeout() == std::chrono::milliseconds(0));
}
{
wc.majority(std::chrono::milliseconds(5000));
coll.write_concern(wc);
EXPECT(coll.write_concern() == wc);
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() == rp);
EXPECT(coll.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary);
}
{
rp.mode(mongocxx::read_preference::read_mode::k_secondary);
coll.read_preference(rp);
EXPECT(coll.read_preference() == rp);
}
}
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);
}
{
auto result_opt =
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;
auto result = indexes.create_many(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);
indexes.drop_one("custom_name");
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);
indexes.drop_all();
EXPECT(count_indexes() == 1);
}
Document Operations
Query the Number of Documents
Estimate the Number of Documents
EXPECT(coll.estimated_document_count() == 3);
{
EXPECT(coll.estimated_document_count(opts) == 3);
}
}
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(coll.count_documents(x1.view()) == 1);
auto result_opt = coll.delete_one(x1.view());
EXPECT(result_opt);
EXPECT(result.deleted_count() == 1);
EXPECT(result.result().deleted_count() == 1);
EXPECT(coll.count_documents(x1.view()) == 0);
}
{
EXPECT(coll.count_documents(x2.view()) == 1);
EXPECT(coll.delete_one(x2.view(), opts));
EXPECT(coll.count_documents(x2.view()) == 0);
}
EXPECT(coll.count_documents(x3.view()) == 1);
}
Delete Many Documents
{
EXPECT(coll.count_documents(x1.view()) == 1);
EXPECT(coll.count_documents(x2.view()) == 1);
EXPECT(coll.count_documents(x3.view()) == 1);
EXPECT(result_opt);
EXPECT(result.deleted_count() == 2);
EXPECT(result.result().deleted_count() == 2);
EXPECT(coll.count_documents(x1.view()) == 1);
EXPECT(coll.count_documents(x2.view()) == 0);
EXPECT(coll.count_documents(x3.view()) == 0);
}
{
EXPECT(coll.count_documents(x1.view()) == 1);
EXPECT(coll.count_documents(x1.view()) == 0);
}
}
Insert a Document
{
EXPECT(coll.count_documents(x1.view()) == 0);
auto result_opt = coll.insert_one(x1.view());
EXPECT(result_opt);
EXPECT(result.result().inserted_count() == 1);
EXPECT(coll.count_documents(x1.view()) == 1);
}
{
EXPECT(coll.count_documents(x2.view()) == 0);
EXPECT(coll.insert_one(x2.view(), opts));
EXPECT(coll.count_documents(x2.view()) == 1);
}
}
Insert Many Documents
{
std::vector<bsoncxx::document::view> docs = {x1.view(), x2.view()};
auto result_opt = coll.insert_many(docs);
EXPECT(result_opt);
EXPECT(result.inserted_count() == 2);
EXPECT(result.result().inserted_count() == 2);
}
{
auto result_opt = coll.insert_many(std::begin(docs), std::end(docs));
EXPECT(result_opt);
EXPECT(result.inserted_count() == 2);
EXPECT(result.result().inserted_count() == 2);
}
{
std::array<bsoncxx::document::view, 2> docs = {z1.view(), z2.view()};
EXPECT(coll.insert_many(docs, opts));
}
}
Replace a Document
{
EXPECT(coll.count_documents(x0.view()) == 0);
auto result_opt = coll.replace_one(filter.view(), x0.view());
EXPECT(result_opt);
EXPECT(result.matched_count() == 1);
EXPECT(result.modified_count() == 1);
EXPECT(result.result().matched_count() == 1);
EXPECT(result.result().modified_count() == 1);
EXPECT(coll.count_documents(x0.view()) == 1);
}
{
EXPECT(coll.count_documents(is_original.view()) == 2);
EXPECT(coll.count_documents(is_replaced.view()) == 1);
opts.upsert(true);
EXPECT(coll.replace_one(filter.view(), x0.view(), opts));
EXPECT(coll.count_documents(is_original.view()) == 1);
EXPECT(coll.count_documents(is_replaced.view()) == 2);
EXPECT(coll.replace_one(filter.view(), x0.view(), opts));
EXPECT(coll.count_documents(is_original.view()) == 0);
EXPECT(coll.count_documents(is_replaced.view()) == 3);
EXPECT(coll.replace_one(filter.view(), x0.view(), opts));
EXPECT(coll.count_documents(is_original.view()) == 0);
EXPECT(coll.count_documents(is_replaced.view()) == 4);
}
}
Update a Document
{
EXPECT(coll.count_documents(updated.view()) == 0);
auto result_opt = coll.update_one(filter.view(), update.view());
EXPECT(result_opt);
EXPECT(result.matched_count() == 1);
EXPECT(result.modified_count() == 1);
EXPECT(result.result().matched_count() == 1);
EXPECT(result.result().modified_count() == 1);
EXPECT(coll.count_documents(updated.view()) == 1);
}
{
EXPECT(coll.count_documents(updated.view()) == 1);
EXPECT(coll.count_documents(x4.view()) == 0);
opts.upsert(true);
opts));
EXPECT(coll.count_documents(x4.view()) == 1);
EXPECT(coll.count_documents(updated.view()) == 2);
}
}
Update Multiple Documents
{
EXPECT(coll.count_documents(updated.view()) == 0);
auto result_opt = coll.update_many(filter.view(), update.view());
EXPECT(result_opt);
EXPECT(result.matched_count() == 2);
EXPECT(result.modified_count() == 2);
EXPECT(result.result().matched_count() == 2);
EXPECT(result.result().modified_count() == 2);
EXPECT(coll.count_documents(updated.view()) == 2);
}
{
EXPECT(coll.count_documents(original.view()) == 1);
EXPECT(coll.count_documents(updated.view()) == 2);
opts.upsert(true);
EXPECT(coll.update_many(original.view(), update.view(), opts));
EXPECT(coll.count_documents(original.view()) == 0);
EXPECT(coll.count_documents(updated.view()) == 3);
EXPECT(coll.update_many(original.view(), update.view(), opts));
EXPECT(coll.count_documents(original.view()) == 0);
EXPECT(coll.count_documents(updated.view()) == 4);
}
}
Find and Delete a Document
{
EXPECT(coll.count_documents(has_field_x.view()) == 3);
auto result_opt = coll.find_one_and_delete(x2.view());
EXPECT(result_opt);
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
EXPECT(coll.count_documents(has_field_x.view()) == 2);
}
{
EXPECT(coll.count_documents(has_field_x.view()) == 2);
auto result_opt = coll.find_one_and_delete(x3.view(), opts);
EXPECT(result_opt);
EXPECT(*result_opt == x3);
EXPECT(coll.count_documents(has_field_x.view()) == 1);
}
EXPECT(coll.count_documents(x1.view()) == 1);
}
Find and Replace a Document
{
EXPECT(coll.count_documents(x0.view()) == 0);
auto result_opt = coll.find_one_and_replace(x2.view(), x0.view());
EXPECT(result_opt);
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
EXPECT(coll.count_documents(x0.view()) == 1);
}
{
EXPECT(coll.count_documents(x0.view()) == 1);
auto result_opt = coll.find_one_and_replace(x3.view(), x0.view(), opts);
EXPECT(result_opt);
EXPECT(*result_opt == x3);
EXPECT(coll.count_documents(x0.view()) == 2);
}
EXPECT(coll.count_documents(x1.view()) == 1);
}
Find and Update a Document
{
EXPECT(coll.count_documents(x2.view()) == 1);
EXPECT(coll.count_documents(updated.view()) == 0);
auto result_opt = coll.find_one_and_update(x2.view(), update.view());
EXPECT(result_opt);
EXPECT(doc["_id"]);
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
EXPECT(coll.count_documents(x2.view()) == 1);
EXPECT(coll.count_documents(updated.view()) == 1);
}
{
EXPECT(coll.count_documents(x3.view()) == 1);
EXPECT(coll.count_documents(updated.view()) == 1);
auto result_opt = coll.find_one_and_update(x3.view(), update.view(), opts);
EXPECT(result_opt);
EXPECT(*result_opt == x3);
EXPECT(coll.count_documents(x3.view()) == 1);
EXPECT(coll.count_documents(updated.view()) == 2);
}
EXPECT(coll.count_documents(x1.view()) == 1);
}
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);
}
{
opts.ordered(true);
}
}
Execute Multiple Bulk Write Operations
bulk_write.append(insert);
bulk_write.append(update);
auto result_opt = bulk_write.execute();
EXPECT(result_opt);
EXPECT(result.inserted_count() == 1);
EXPECT(result.modified_count() == 1);
}
Execute an Aggregation Operation
{
pipeline.sample(1);
return doc.find("x") != doc.end();
};
EXPECT(std::count_if(cursor.begin(), cursor.end(), has_field_x) == 1);
}
{
EXPECT(std::distance(cursor.begin(), cursor.end()) == 2);
}
}
Error Handling
Invalid Collection
{
wc.acknowledge_level(mongocxx::write_concern::level::k_unacknowledged);
opts.write_concern(wc);
}
try {
auto name = coll.find_one_and_update(empty.view(), empty.view(), opts);
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}
Invalid Parameter
opts.max_await_time(std::chrono::milliseconds(-1));
try {
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}
Incompatible Options
{
wc.acknowledge_level(mongocxx::write_concern::level::k_unacknowledged);
opts.write_concern(wc);
}
try {
auto name = coll.find_one_and_update(empty.view(), empty.view(), opts);
EXPECT(false && "should not reach this point");
EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter);
}
}