Data stored in MongoDB as follows:
Followed the approach as mentioned Getting Started with MongoDB and C++ | MongoDB to build C++ drivers for MongoDB.
Here is my C++ code snippet
#include
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
int main()
{
std::cout << “Hello World!\n”;
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
auto db = client["test"];
auto collection = db["inventory"];
// Find All Documents in a Collection
{
auto cursor_all = collection.find({});
for (auto&& doc : cursor_all) {
// Do something with doc
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
}
Expected output should be the data available in the MongoDB collection. Unfortunately the actual result displayed as the following
In the same example,
bsoncxx::document::element model_bson = doc[“model”];
std::string modelName = std::string(model_bson .get_utf8().value);
std::cout << modelName << std::endl; // value prints good and it is as per the database
bsoncxx::document::element price_bson = doc[“price”];
double dVal = price_bson .get_double(); // ----- application crashing
std::cout << dVal << std::endl;
Looking for the help in these issues please.