首页 文章

将JSON插入Mongocxx

提问于
浏览
2

我目前正在尝试将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 回答

  • 4

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

相关问题