earnric
earnric

Reputation: 534

Processing MONGO record with nested arrays in C++

I'm trying to use the MONGO C++ API to process a bunch of records that look like the following... The number of lines in the "Entries" array is variable: it's either 13 or 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" } ] ] }

I query the collection based on UTCTime and SvId ... when I get the records back, I'm unsure of how to step thru them all...

Normally, I just get a cursor and loop through the set of returned records with "next()" ... but now I haven an "Entries" field that has either 7 or 13 entries. How do I access each of those items? I'm guessing there must be somekind of "subcursor" that I can use to loop thru those.

I'm looking through the API and examples, but there's not a lot on nested arrays.

Thanks,

Rick

Upvotes: 0

Views: 944

Answers (1)

echo
echo

Reputation: 104

Here there is a great example how to use a arrays with de MongoDB API.

EDIT

I build an example:

#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;
}

Output:

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 }

I hope that's what you're looking for! Let me know!

Upvotes: 1

Related Questions