我目前正在尝试将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.
///
如何将我的JSON文件转换为 stdx::string_view
?
谢谢!
1 回答
bsoncxx::stdx::string_view
可以从std::string
构造 . 只需将文件内容(假设它包含一个JSON对象)加载到std::string
(可能是通过std::ifstream
),然后将std::string
传递给bsoncxx::from_json
. 从bsoncxx::from_json
返回的对象是bsoncxx::document::value
,它是包含BSON文档的资源拥有类型 .