2

I have the following document in my MongoDB test database:

> db.a.find().pretty()
{
    "_id" : ObjectId("5113d680732fb764c4464fdf"),
    "x" : [
            {
                "a" : 1,
                "b" : 2
            },
            {
                "a" : 3,
                "b" : 4
            }
          ]
}

I'm trying to access and process the elements in the "x" array. However, it seems that the Mongo driver is identifying it not as an array of JSON document, but as Date type, as shown in the following code:

auto_ptr<DBClientCursor> cursor = c.query("test.a", BSONObj());
while (cursor->more()) {      
  BSONObj r = cursor->next();
  cout << r.toString() << std::endl;
}

which output is:

{ _id: ObjectId('51138456732fb764c4464fde'), x: new Date(1360233558334) }

I'm trying to follow the documentation in http://api.mongodb.org/cplusplus and http://docs.mongodb.org/ecosystem/drivers/cpp-bson-array-examples/, but it is quite poor. I have found other examples of processing arrays, but always with simple types (e.g. array of integer), but not when the elements in the array are BSON documents themselves.

Do you have some code example of procesing arrays which elements are generic BSON elements, please?

1
  • Moreover, the ObjectID that mongo shell and the trace shows seem not being the same. Quite weird... Commented Feb 8, 2013 at 8:09

2 Answers 2

3

you could use the .Array() method or the getFieldDotted() method: as in the following:

Query query = Query();
auto_ptr<DBClientCursor> cursor = myConn.query("test.a", query);

while( cursor->more() ) {
    BSONObj nextObject = cursor->next();

    cout << nextObject["x"].Array()[0]["a"] << endl;
    cout << nextObject.getFieldDotted("x.0.a") << endl;


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

1 Comment

I finally found a solution, but thank you anyway for the hint! I didn't know about the [ ] notation in this case and the getFieldDotted() method.
2

At the end, it seems that embeddedObject() method was the key:

auto_ptr<DBClientCursor> cursor = c.query("test.a", BSONObj());
while (cursor->more()) { 
  BSONObj r = cursor->next();
  cout << "Processing JSON document: " << r.toString() << std::endl;
  std::vector<BSONElement> be = r.getField("x").Array();
  for (unsigned int i = 0; i<be.size(); i++) {
      cout << "Processing array element: " << be[i].toString() << std::endl;
      cout << "                 of type: " << be[i].type() << std::endl;
      BSONObj bo = be[i].embeddedObject();
      cout << "Processing a field: " << bo.getField("a").toString() << std::endl;
      cout << "Processing b field: " << bo.getField("b").toString() << std::endl;
  }
 }

I was wrongly retrieving a different ObjectID and a different type (Date instead of array) becuase I was looking to a different collection :$

Sorry for the noise. I hope that the fragment above helps others to figure out how to manipulate arrays using the MongoDB C++ driver.

Comments

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.