mongodb 使用 C++ 遗留驱动程序的查询导致 BSONElement:错误类型 -64

mongodb query with c++ legacy driver results in BSONElement: bad type -64

我正在尝试使用 c++ mongo 驱动程序处理 mongodb 集合中的数组。但是我总是以错误消息结尾:

terminate called after throwing an instance of mongo::MsgAssertionException'
  what():  BSONElement: bad type -64
Aborted (core dumped)

我试图在 google 上找到它,但如果找到,唯一的答案是告诉我修复我的数据库。我这样做了,但错误仍然存​​在。

我真的被这个错误搞糊涂了。 mongo-shell 打印的集合似乎没有损坏,shell 也没有声称有任何错误(见下文)。所以我假设在我的代码中生成的向量 rawDataArray (另见下文)包含有效数据。但是,当我尝试打印这些值时,我收到了上述错误消息。

我做错了什么?

非常感谢

迈克尔

PS:

我正在使用 mongodb C++ 遗留驱动程序,错误是由以下代码产生的:

mongo::DBClientConnection connection;
connection.connect("localhost");

mongo::BSONObj resultObj;
try 
{  
    std::auto_ptr<mongo::DBClientCursor> cursor = 
        connection.query("mydb.results", 
                MONGO_QUERY( "_id" << mongo::OID(resultID) ) );
    while (cursor->more())
    {
        resultObj = cursor->next();
    }
}
catch( const mongo::DBException &e ) 
{
    // error handling
}

// extract raw_data array
std::vector<mongo::BSONElement> rawDataArray;
if( resultObj.hasField("raw_data") )
{
    rawDataArray = resultObj["raw_data"].Array();
}
else
{
    // error handling
}

for(auto const & data : rawDataArray)
{
    std::cout << data << std::endl;

}

集合看起来像:

db.getCollection('results').find({}).pretty()
{
    "_id" : ObjectId("56cf1315f7e0583e2c4ec702"),
    "experiment_id" : ObjectId("56c5b8e7e1fa370a1de9d06f"),
    "module_id" : ObjectId("56c5b8e7e1fa370a1de9d06e"),
    "raw_data" : [ 
        {
            "id_number" : "0accb65f4fc311",
            "box" : "0accb65f4fc3",
            "paper" : 1,
            "seed" : 1,
            "length" : 0,
            "time" : ISODate("2015-09-15T20:00:00.000Z")
        }, 
        {
            "id_number" : "0accb65f4fc312",
            "box" : "0accb65f4fc3",
            "paper" : 1,
            "seed" : 2,
            "length" : 0,
            "time" : ISODate("2015-09-15T20:00:00.000Z")
        }, 
        {
            "id_number" : "0accb65f4fc313",
            "box" : "0accb65f4fc3",
            "paper" : 1,
            "seed" : 3,
            "length" : 0,
            "time" : ISODate("2015-09-15T20:00:00.000Z")
        }, 
        ... skipped some data here, there are 204 nearly identical elements
        {
            "id_number" : "0accb65f4fc3451",
            "box" : "0accb65f4fc3",
            "paper" : 4,
            "seed" : 51,
            "length" : 0,
            "time" : ISODate("2015-09-15T20:00:00.000Z")
        }
    ],
    "processed_data" : []
}

您在销毁游标对象后访问resultObj,但resultObj 只是查看游标拥有的数据。如果您需要 BSONObj 的生命周期超过返回它的游标的生命周期,请调用 BSONObj 上的 getOwned 以获取拥有的副本。