Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9279317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:36:12+00:00 2026-06-18T17:36:12+00:00

I have the following document in my MongoDB test database: > db.a.find().pretty() { _id

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:36:14+00:00Added an answer on June 18, 2026 at 5:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following document in a couchdb database: { _id: 000013a7-4df6-403b-952c-ed767b61554a, _rev: 1-54dc1794443105e9d16ba71531dd2850,
I have the following data structure in MongoDB: { _id : ObjectId( xy ),
I have the following document structure inside a mongodb collection: { _id: XXX, Name:
In MongoDB, I have the following document { _id: { $oid : 4FFD813FE4B0931BDAAB4F01 },
I have the following document: {_id: '4eb79ee1e60fc603788e7259', Name: 'name', Subsidiaries: [ { _id: '4eb79eeae60fc603788e7271',
I have the following documents in my MongoDB-database: {stats: [2,5,1]} {stats: [1,9,2]} Now I
I'm trying to retrieve a document stored in MongoDB using its _id of the
I have a MongoDB collection with documents in the following format: { _id :
I'm using MongoDB and Spring 3. I have the following domain object: @Document(collection =
I used rails3.2.3 with mongoid gem for mongodb database. I have following code user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.