I installed mongocxx driver successfully. Now I tried to write a class to connect and query data from database. If I write a query in constructor like this
DBConnection::DBConnection()
{
mongocxx::instance instance{};
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(mongocxx::uri{});
coll = client["testdb"]["testcollection"];
auto curs = coll.find(bsoncxx::builder::stream::document{} << finalize);
for (auto doc: curs) {
std::cout << bsoncxx::to_json(doc) << "\n";
}
}
it works like a charm.
But, if I separate in two functions like this
DBConnection::DBConnection()
{
mongocxx::instance instance{};
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(mongocxx::uri{});
coll = client["testdb"]["testcollection"];
}
void DBConnection::loadData() {
mongocxx::cursor cursor = coll.find({});
for (auto doc: cursor) {
std::cout << bsoncxx::to_json(doc) << "\n";
}
}
then, it gave the error: src/mongoc/mongoc-topology-scanner.c:754 mongoc_topology_scanner_get_error(): precondition failed: ts
.
I don't know why. How can I fix this?