我正在尝试使用MONGO C ++ API来处理一堆看起来如下的记录......“条目”数组中的行数是可变的:它是13或7。
{ "_id" : ObjectId("541af7a4c9c7450a5a5c7e8e"), "SvId" : "SV120", "UTCTime" : "2014-09-18T15:17:56.541Z", "Interval" : 10, "HPLANC" : "DownlinkA",
"Entries" : [
[ { "IPAddress" : "172.20.10.20" }, { "Port" : 4096 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.10.20" }, { "Port" : 4097 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.10.20" }, { "Port" : 4098 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.10.20" }, { "Port" : 4099 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.10.20" }, { "Port" : 4103 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.100.10" }, { "Port" : 4102 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.100.10" }, { "Port" : 4104 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.150.10" }, { "Port" : 4100 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.200.10" }, { "Port" : 4100 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.200.10" }, { "Port" : 4150 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.200.10" }, { "Port" : 4151 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.200.10" }, { "Port" : 4152 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ],
[ { "IPAddress" : "172.20.200.10" }, { "Port" : 4153 },
{ "MessageCount" : "0" }, { "ByteCount" : "0" } ] ] }
我根据UTCTime和SvId查询集合...当我收回记录时,我不确定如何通过它们全部步骤......
通常情况下,我只是获取一个游标并使用“next()”循环返回记录集...但是现在我还有一个“条目”字段,其中包含7个或13个条目。我如何访问这些项目?我猜我必须有一些“subcursor”,我可以通过它来循环。
我正在查看API和示例,但在嵌套数组上没有太多。
谢谢,
瑞克
答案 0 :(得分:1)
Here有一个很好的例子,说明如何将数组与de MongoDB API一起使用。
修改强>
我建立了一个例子:
#include "mongo/bson/bson.h"
#include <iostream>
#include <list>
#include <vector>
using mongo::BSONArray;
using mongo::BSONArrayBuilder;
using mongo::BSONObj;
using mongo::BSONObjBuilder;
using mongo::BSONElement;
using namespace std;
int main() {
// Build an object
BSONObjBuilder bob;
// Build a array
BSONArray arr = BSON_ARRAY(
BSON_ARRAY( BSON( "IPAddress" << "172.20.10.20") << BSON( "Port" << 4096) << BSON( "MessageCount" << 0) << BSON( "ByteCount" << 0) ) <<
BSON_ARRAY( BSON( "IPAddress" << "172.20.10.10") << BSON( "Port" << 4100) << BSON( "MessageCount" << 0) << BSON( "ByteCount" << 0) ) <<
BSON_ARRAY( BSON( "IPAddress" << "172.20.10.10") << BSON( "Port" << 4150) << BSON( "MessageCount" << 0) << BSON( "ByteCount" << 0) ) <<
BSON_ARRAY( BSON( "IPAddress" << "172.20.10.10") << BSON( "Port" << 4152) << BSON( "MessageCount" << 0) << BSON( "ByteCount" << 0) )
);
bob.appendArray("Entries", arr);
// Create the object
BSONObj an_obj = bob.obj();
cout << "BSON: "<< an_obj << endl;
// Print the array out
vector<BSONElement> array = an_obj["Entries"].Array();
for (vector<BSONElement>::iterator ar = array.begin(); ar != array.end(); ++ar){
cout << *ar << endl;
vector<BSONElement> elem = ar->Array();
for (vector<BSONElement>::iterator it = elem.begin(); it != elem.end(); ++it){
cout << *it << endl;
}
}
cout << endl;
return 0;
}
<强>输出:强>
BSON: { Entries: [ [ { IPAddress: "172.20.10.20" }, { Port: 4096 }, { MessageCount: 0 }, { ByteCount: 0 } ], [ { IPAddress: "172.20.10.10" }, { Port: 4100 }, { MessageCount: 0 }, { ByteCount: 0 } ], [ { IPAddress: "172.20.10.10" }, { Port: 4150 }, { MessageCount: 0 }, { ByteCount: 0 } ], [ { IPAddress: "172.20.10.10" }, { Port: 4152 }, { MessageCount: 0 }, { ByteCount: 0 } ] ] }
0: [ { IPAddress: "172.20.10.20" }, { Port: 4096 }, { MessageCount: 0 }, { ByteCount: 0 } ]
0: { IPAddress: "172.20.10.20" }
1: { Port: 4096 }
2: { MessageCount: 0 }
3: { ByteCount: 0 }
1: [ { IPAddress: "172.20.10.10" }, { Port: 4100 }, { MessageCount: 0 }, { ByteCount: 0 } ]
0: { IPAddress: "172.20.10.10" }
1: { Port: 4100 }
2: { MessageCount: 0 }
3: { ByteCount: 0 }
2: [ { IPAddress: "172.20.10.10" }, { Port: 4150 }, { MessageCount: 0 }, { ByteCount: 0 } ]
0: { IPAddress: "172.20.10.10" }
1: { Port: 4150 }
2: { MessageCount: 0 }
3: { ByteCount: 0 }
3: [ { IPAddress: "172.20.10.10" }, { Port: 4152 }, { MessageCount: 0 }, { ByteCount: 0 } ]
0: { IPAddress: "172.20.10.10" }
1: { Port: 4152 }
2: { MessageCount: 0 }
3: { ByteCount: 0 }
我希望你能找到的是什么!让我知道!