将JSON插入Mongocxx

zer*_*ool 2 c++ json mongodb bson mongo-cxx-driver

我目前正在尝试将JSON文件插入到我的mongoDB中。我已经看到过去已经通过使用mongo :: BSONObj解决了这个问题……但这似乎不是一个选择,因为他们发布了适用于c ++ 11的新mongocxx驱动程序。这是我在bsoncxx src文件中找到的:

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.
///
Run Code Online (Sandbox Code Playgroud)

如何将JSON文件放入stdx::string_view

谢谢!

acm*_*acm 5

bsoncxx::stdx::string_view可以从构成std::string。只需将文件内容(假设它包含一个JSON对象)加载到中std::string(可能通过std::ifstream),然后将其传递std::string给即可bsoncxx::from_json。从返回的对象bsoncxx::from_jsonbsoncxx::document::value,这是一种资源拥有类型,其中包含BSON文档。