gpt4 book ai didi

c++ - 在 C++ 中使用嵌套数组处理 MONGO 记录

转载 作者:行者123 更新时间:2023-11-28 06:41:10 25 4
gpt4 key购买 nike

我正在尝试使用 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 个条目的“条目”字段。我如何访问这些项目中的每一个?我猜一定有某种“子光标”可以用来遍历它们。

我正在查看 API 和示例,但关于嵌套数组的内容并不多。

谢谢,

瑞克

最佳答案

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 }

我希望这就是您要找的!让我知道!

关于c++ - 在 C++ 中使用嵌套数组处理 MONGO 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25917912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com