|
Hi puya@motionmetrics.com.
For those operations, if an error occurs, a mongocxx::exception::bulk_write_exception is thrown.
try {
|
auto result = coll.insert_one(make_document(kvp("a", 1)));
|
// If no exception was thrown, result is necessarily set.
|
} catch (mongocxx::bulk_write_exception& bwe) {
|
std::cout << "error occurred" << std::endl;
|
}
|
If an unacknowledged write concern is used, the server does not send back a reply, so stdx::nullopt is returned. A mongocxx::exception::bulk_write_exception could still be returned on a client error (e.g. a socket timeout).
mongocxx::write_concern wc_unacknowledged;
|
mongocxx::options::insert opts;
|
opts.write_concern (wc_unacknowledged);
|
try {
|
auto result = coll.insert_one(make_document(kvp("a", 1)), opts);
|
} catch (mongocxx::bulk_write_exception& bwe) {
|
std::cout << "error occurred" << std::endl;
|
}
|
So, handling the exception should be the only requirement for checking if the operation succeeded. If using an unacknowledged write concern, the operation could have failed server-side, but using an unacknowledged write concern is opting into that fire-and-forget behavior.
The documentation for insert/update/delete collections functions refers to this behavior, e.g. for collection::insert_one
Returns
The optional result of attempting to perform the insert. If the write concern is unacknowledged, the optional will be disengaged.
Exceptions
mongocxx::bulk_write_exception if the operation fails.
|