c – 如何解析聚合管道结果?
发布时间:2020-12-16 06:51:33 所属栏目:百科 来源:网络整理
导读:我在C中使用聚合管道并在调用之后 runCommand("Database",BSON("aggregate" "collection" "pipeline" pipeline),res); res包含以下BSON: { waitedMS: 0,result: [ { _id: ObjectId('564ef990bfd82315fc8db2bc'),xIndex: 139 },{ _id: ObjectId('564ef990bfd
我在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'),{ _id: ObjectId('564ef991bfd82315fc8db2c6'),{ _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会添加数组索引? 解决方法
我找到了解决方案:
BSONObj bdoc = result[0].embeddedObject(); 现在,std :: cout<< bdoc.toString()<<的std :: ENDL;提供了更方便的输出:{_ id:ObjectId('564ef990bfd82315fc8db2bc'),xIndex:139} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |