C++ Mongocxx库分段错误

C++ Mongocxx库分段错误,c++,mongodb,mongo-cxx-driver,mongo-c-driver,C++,Mongodb,Mongo Cxx Driver,Mongo C Driver,我试图在Docker映像中安装mongo-c-driver和mongo cxx驱动程序,但当我运行c++程序时,它抛出了一个分段错误。有什么想法吗?Valgrind说这是mongo的错 我有一些物联网设备,在我自己的电脑里运行得很好 我正在使用: ubuntu:focal作为基本图像 mongo-c-driverversion1.16.2(还尝试了1.17和1.15) mongo cxx驱动程序version3.5(也使用3.6进行了尝试) MongoDB服务器版本4.2.8 g++versi

我试图在Docker映像中安装
mongo-c-driver
mongo cxx驱动程序
,但当我运行
c++
程序时,它抛出了一个分段错误。有什么想法吗?Valgrind说这是mongo的错

我有一些物联网设备,在我自己的电脑里运行得很好

我正在使用:

  • ubuntu:focal
    作为基本图像
  • mongo-c-driver
    version
    1.16.2
    (还尝试了
    1.17
    1.15
  • mongo cxx驱动程序
    version
    3.5
    (也使用
    3.6
    进行了尝试)
  • MongoDB服务器版本
    4.2.8
  • g++
    version
    (Ubuntu 9.3.0-10ubuntu2)9.3.0
代码:

std::string MongoDB::insertDocument(std::string数据库,
std::字符串集合,bsoncxx::文档::视图(或值文档)
{
尝试
{
mongocxx::uri(\u连接\u字符串);
mongocxx::客户端连接(uri);//此处发生seg错误
mongocxx::数据库db=conn[database];
mongocxx::collection coll=db[collection];
自动插入=coll.insert_one(document.view());
bsoncxx::oid oid=inserted->inserted_id().get_oid().value;
返回oid.to_string();
}
捕获(const mongocxx::异常&e)
{
标准::cerr来自:

重要提示:在建立任何连接之前,您需要创建一个且仅创建一个的实例。此实例必须存在于整个程序中

因此,您可以将其添加为
MongoDB
类的
static
成员:

标题

class MongoDB {
    //...
private:
     static mongocxx::instance inst;
};
impl

mongocxx::instance MongoDB::inst{};
下面是一个示例,我连接到默认的mongodb服务器(
mongodb://localhost:27017
),使用以前使用
mongo
cli创建的数据库
dbted
和收藏
movie

我添加了一个方法,可以在集合中的所有文档中获取
mongocxx::cursor

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

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

#include <iostream>

class MongoDB {
public:
    MongoDB() :
        client{mongocxx::uri{"mongodb://localhost:27017"}},
        db{client["dbted"]},
        coll{db["movie"]} 
    {}

    mongocxx::cursor all() { return coll.find({}); }

private
    mongocxx::client client;
    mongocxx::database db;
    mongocxx::collection coll;

    static mongocxx::instance inst;
};

mongocxx::instance MongoDB::inst{};

int main() {
    try {
        MongoDB db;

        std::cout << "Connected successfully\n";

        for(auto doc : db.all()) {
            std::cout << bsoncxx::to_json(doc) << '\n';
        }

    } catch(const std::exception& ex) {
        std::cerr << "Exception: " << ex.what() << std::endl;
        return 1;
    }
}

#包括
#包括
#包括
#包括
#包括
#包括
#包括
MongoDB类{
公众:
MongoDB():
客户端{mongocxx::uri{”mongodb://localhost:27017"}},
db{client[“dbted”]},
coll{db[“电影”]}
{}
mongocxx::cursor all(){return coll.find({});}
私有的
mongocxx::客户端;
mongocxx::数据库数据库数据库;
mongocxx::集合coll;
静态mongocxx::实例inst;
};
mongocxx::实例MongoDB::inst{};
int main(){
试一试{
MongoDB;

STD::“当我运行C++程序时,它会造成一个分段错误。”——你能创建一个并与我们共享吗?抱歉,我刚刚添加了更多的信息。我可以确认在找到解决方案之前我也得到了同样的错误(我把它放在我的答案里)。@ LucasHagen,欢迎你!我花了一些时间来找到它。
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

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

#include <iostream>

class MongoDB {
public:
    MongoDB() :
        client{mongocxx::uri{"mongodb://localhost:27017"}},
        db{client["dbted"]},
        coll{db["movie"]} 
    {}

    mongocxx::cursor all() { return coll.find({}); }

private
    mongocxx::client client;
    mongocxx::database db;
    mongocxx::collection coll;

    static mongocxx::instance inst;
};

mongocxx::instance MongoDB::inst{};

int main() {
    try {
        MongoDB db;

        std::cout << "Connected successfully\n";

        for(auto doc : db.all()) {
            std::cout << bsoncxx::to_json(doc) << '\n';
        }

    } catch(const std::exception& ex) {
        std::cerr << "Exception: " << ex.what() << std::endl;
        return 1;
    }
}