0

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

1
  • The only thing that seems to get me close (and doesn't throw an exception) is accessing the BSONObj returned buy the query (and subsequent next() ) is <code> dsuPoint.getFieldDotted("Entries.0.0.IPAddress") </code> which return "EOO" ... ??? Commented Sep 18, 2014 at 21:31

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thx...But I had found that too. It really doesn't help with a doubly nested array -- which you would think just requires an extra level of indirection. However, it doesn't... I haven an array of arrays -- and accessing via Array().Array() .. or even Array()[0].Array() doesn't seem to work. I've been experimenting with all the permutations, but none work on this collection. They all throw an std exception.
Last night I build an example, see my first post! Let me know if it's you looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.