I'm currently trying to insert a JSON file into my mongoDB. I've already seen that this was solved by making use of the mongo::BSONObj in the past... But this doesnt seem to be an option since they released the new mongocxx driver for c++11. This is what I found in the bsoncxx src files:
BSONCXX_API document::value BSONCXX_CALL from_json(stdx::string_view json);
/// Constructs a new document::value from the provided JSON text
///
/// @param 'json'
/// A string_view into a JSON document
///
/// @returns A document::value if conversion worked.
///
/// @throws bsoncxx::exception with error details if the conversion failed.
///
How do I get my JSON file into a stdx::string_view
?
Thanks!
A bsoncxx::stdx::string_view
can be constructed from a std::string
. Simply load your file contents (assuming it contains a single JSON object) into a std::string
(perhaps via std::ifstream
), and pass that std::string
to bsoncxx::from_json
. The object returned from bsoncxx::from_json
is a bsoncxx::document::value
, which is a resource-owning type that contains a BSON document.