Use a mongocxx::client_session for a sequence of operations with causal consistency.
#include <iostream>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
int main() {
auto session = conn.start_session();
auto coll = conn["db"]["collection"];
auto result = coll.update_one(session,
make_document(kvp("_id", 1)),
make_document(kvp("$inc", make_document(kvp("x", 1)))));
std::cout << "Updated " << result->modified_count() << " documents" << std::endl;
auto cursor = coll.find(session, make_document(kvp(
"_id", 1)), opts);
}
}
std::string to_json(document::view view, ExtendedJsonMode mode=ExtendedJsonMode::k_legacy)
Converts a BSON document to a JSON string, in extended format.