MongoDB C ++驱动程序断言失败

时间:2013-02-27 21:18:09

标签: c++ mongodb exception insert assertion

问题

当我尝试使用C ++驱动程序将文档插入MongoDB时,我收到以下异常消息:

Wed Feb 27 15:21:38   Assertion failure p src/mongo/client/dbclientinterface.h 1096
0 assertion src/mongo/client/dbclientinterface.h:1096

据我所知,它似乎与端口号有关? dbclientinterface.h:1096包含以下行:

MessagingPort& port() { verify(p); return *p; } 

设置连接(main.cpp)

mongo::DBClientConnection DBConn( "localhost" );
mongo::DBClientConnection DBConn( "localhost:27017" ); // I've also tried this...

插入文档(different_file.h)

while( m_Entries.size() ){
    JsonBox::Value Data( m_Entries.front() );

    try {
        std::stringstream   JSONDoc;
        mongo::BSONObj      BSONDoc;

        Data["doc"].writeToStream( JSONDoc, false );
        BSONDoc = mongo::fromjson( JSONDoc.str() );

        // std::cout << Data["ns"].getString() << std::endl;
        // std::cout << BSONDoc.toString() << std::endl;

        // This is where the exception is thrown...
        m_DBConn.insert( Data["ns"].getString(), BSONDoc );

    } catch( const mongo::DBException& e ){
        std::cout << e.toString() << std::endl;

    }

    m_EntriesMutex.lock();
    m_Entries.pop();
    m_EntriesMutex.unlock();

}

1 个答案:

答案 0 :(得分:1)

我在文档中简要地挖了一下,偶然发现了一个令人吃惊的事实,即无法从构造函数连接到mongodb数据库。我不得不改变这个:

mongo::DBClientConnection DBConn( "localhost" );

到此:

mongo::DBClientConnection DBConn;

DBConn.connect( "localhost" );