c++ - MongoDB C++ 教程程序失败 : 'mongocxx::v_noabi::logic_error'

标签 c++ mongodb c++11 mongo-cxx-driver

我正在尝试使用 C++ 和 MongoDB 完成一些工作。到目前为止,出现了无数问题,但我都挺过来了。

然后我得到了这个:

terminate called after throwing an instance of 'mongocxx::v_noabi::logic_error'
  what():  invalid use of default constructed or moved-from mongocxx::client object
Aborted

坦率地说,我正在失去希望。这是我试图运行的示例: https://docs.mongodb.com/getting-started/cpp/insert/ .

当我尝试运行已编译的程序时出现错误。我能够很好地编译和运行“hellomongo”示例,因此至少部分驱动程序已正确安装。

我的代码:

#include <chrono>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/types.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;

int main(int, char**)   
{

    mongocxx::instance inst{};
    mongocxx::client conn{};

    auto db = conn["test"];

    bsoncxx::document::value restaurant_doc =
        document{} << "address" << open_document << "street"
                   << "2 Avenue"
                   << "zipcode"
                   << "10075"
                   << "building"
                   << "1480"
                   << "coord" << open_array << -73.9557413 << 40.7720266 << close_array
                   << close_document << "borough"
                   << "Manhattan"
                   << "cuisine"
                   << "Italian"
                   << "grades" << open_array << open_document << "date"
                   << bsoncxx::types::b_date { std::chrono::system_clock::time_point {
    std::chrono::milliseconds { 12323 } } } << "grade"
                   << "A"
                   << "score" << 11 << close_document << open_document << "date"
                   << bsoncxx::types::b_date { std::chrono::system_clock::time_point {
    std::chrono::milliseconds { 12323 } } } << "grade"
                   << "B"
                   << "score" << 17 << close_document << close_array << "name"
                   << "Vella"
                   << "restaurant_id"
                   << "41704620" << finalize;

    // We choose to move in our document here, which transfers ownership to insert_one()
    auto res = db["restaurants"].insert_one(std::move(restaurant_doc));
}

我使用以下命令来编译示例:

c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)

感谢任何帮助!我对 C++ 的经验很少,所以我对可能出现的问题有点迷茫。

最佳答案

正如 acm 所指出的,docs.mongodb.com 上的文档已过时。 Github 示例运行良好。我会将此标记为已回答。

关于c++ - MongoDB C++ 教程程序失败 : 'mongocxx::v_noabi::logic_error' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38845214/

相关文章:

C++ vector 或 Queue 在内存和速度方面构建大 Q

c++ - 从 std::filesystem::directory_iterator() 获取最后一个文件名而不迭代?

node.js - 在heroku上部署后无法读取mongo数据库,返回503错误,但在本地可以工作

python - 如何聚合两个集合,其中一个集合的字段大于另一个集合

c++ - 各种初始化和构造之间的关系?

c++11 - 如果我有 T&& temp = std::move(other);然后在按值接受 T 的函数上使用它

C++ 自定义模板 LinkedList 崩溃添加 std::string

c++ - 使用 auto 初始化 QFile

c - 在 MacOS 上构建 mongodb C 驱动程序

c++ - 使用 C++11 中的 unordered_map 需要哪个 gcc 版本?