代码之家  ›  专栏  ›  技术社区  ›  cylex

将JSON插入Mongocxx

  •  3
  • cylex  · 技术社区  · 8 年前

    我目前正在尝试将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 回复  |  直到 8 年前
        1
  •  6
  •   acm    8 年前

    A. bsoncxx::stdx::string_view 可以从 std::string .只需将文件内容(假设它包含单个JSON对象)加载到 std::字符串 std::ifstream ),并通过 std::字符串 bsoncxx::from_json bsoncxx::from_json bsoncxx::document::value ,它是包含BSON文档的资源拥有类型。