2014-09-18 3 views
0

MONGO C++ API를 사용하여 다음과 같은 일련의 레코드를 처리하려고합니다. "Entries"배열의 행 수는 가변적입니다. 13 7.C++에서 중첩 배열로 MONGO 레코드 처리

{ "_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 개의 항목이있는 "Entries"필드가 있습니다. 각 항목에 어떻게 액세스합니까? 거기에 루프를 돌릴 수있는 "서브 커서"가 있어야합니다.

API와 예제를 살펴보고 있지만 중첩 배열에는 그다지 많은 부분이 없습니다.

감사합니다,

릭 드 MongoDB의 API를 가진 배열을 사용하는 방법 좋은 예가

+0

나에게 가까이가는 것 (그리고 예외를 던지지 않는 것)은 BSONObj가 쿼리를 구매하고 (다음으로 next()를) 반환 한 것을 액세스하는 것이 "EOO"를 반환하는 dsuPoint.getFieldDotted("Entries.0.0.IPAddress") ... ??? – earnric

답변

1

Here.

편집

나는 예를 구축 :

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

내가 그건 당신이 찾고있는 희망! 알려줘!

+0

Thx ... 그러나 나는 그것을 또한 발견했다. 실제로 중첩 된 이중 배열에서는 도움이되지 않습니다.이 배열은 추가 수준의 간접 지정을 필요로한다고 생각합니다. 그러나, 나는 배열의 배열을 가지지 않는다. 그리고 Array(), Array() .. 또는 Array() [0] .Array()를 통해 액세스하는 것은 작동하지 않는 것 같다. 나는 모든 순열에 대해 실험을 해왔지만, 아무도이 컬렉션에 대해 연구하지 않았다. 그들은 모두 표준 예외를 던집니다. – earnric

+0

어젯밤에 예제를 만들었습니다. 첫 번째 게시물을보십시오! 그것이 당신이 찾고 있다면 알려주세요! – echo

+1

Thx! 그거야. – earnric