我在C ++中使用聚合管道并在调用
之后runCommand("Database", BSON("aggregate" << "collection" << "pipeline" << pipeline), res);
res包含以下BSON:
{ waitedMS: 0, result: [ { _id: ObjectId('564ef990bfd82315fc8db2bc'), xIndex: 139 }, { _id: ObjectId('564ef990bfd82315fc8db2bd'), xIndex: 148 }, { _id
: ObjectId('564ef991bfd82315fc8db2c5'), xIndex: 139 }, { _id: ObjectId('564ef991bfd82315fc8db2c6'), xIndex: 148 }, { _id: ObjectId('564ef991bfd82315fc
8db2c7'), xIndex: 148 } ], ok: 1.0 }
现在,我想访问结果数组的每个BSON。但是,在做完
之后std::vector<BSONElement> result = res.getField("result").Array();
std::cout << result[0].toString() << std::endl;
我获得以下输出:
{ 0: { _id: ObjectId('564ef990bfd82315fc8db2bc'), xIndex: 139 } }
如何才能获得BSON { _id: ObjectId('564ef990bfd82315fc8db2bc'), xIndex: 139 }
?
为什么mongo会添加数组索引?
答案 0 :(得分:0)
我找到了解决方案:
git request-pull origin/master file:////yourmachine/yourworkingcopy my-new-feature
现在,BSONObj bdoc = result[0].embeddedObject();
提供了更方便的输出:std::cout << bdoc.toString() << std::endl;