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

标签 c++ arrays mongodb mongo-cxx-driver

我正在尝试使用 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/

相关文章:

c++ - 向前声明类模板显式/部分特化有什么意义?

c++ - (Struct *) 使用属性值名称初始化?

php - 使用 Laravel 获取数组键而不是值

node.js - Sails JS 模型 Rest API

mongodb - 如何在 Rust 中使用 Mongodb::cursor?

node.js - 嵌套 mongodb 查询

c++ - doxygen C++ 内联模板文档

长字符串中的 C++ 字符搜索(随机位置)

c - 将结构体数据分配到数组然后写入 .txt 文件时出现总线错误

javascript - 使用 javascript 变量作为数组键名