mongo c++ driver BSONObj object id serialization problem.

459 views
Skip to first unread message

mayank gupta

unread,
Jan 28, 2012, 4:47:13 AM1/28/12
to mongod...@googlegroups.com

I am using the mongo-cpp driver trying to convert the object ID of a BSON object to a string

BSONObj r=some_function();
BSONElement oi;
r
.getObjectID(oi);
OID o=oi.__oid();
cout
<<"oid:"<<o.toString()<<endl;`

I am sure that the BSONObj has valid data but getting the following error

BSONElement: bad type #somenumber ##

Can you suggest any method to get the object id in string format ?

Eliot Horowitz

unread,
Jan 28, 2012, 8:32:32 AM1/28/12
to mongod...@googlegroups.com
What is "some number"
Are you sure its an OID?
Have you tried printing out r

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mongodb-user/-/lTQmULrioP0J.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

mayank gupta

unread,
Jan 28, 2012, 9:08:52 AM1/28/12
to mongod...@googlegroups.com
Its 40. I have tried printing the object r and everything is printing correctly. I am able to access all other members such as numbers and string properly save the OID.

Eliot Horowitz

unread,
Jan 28, 2012, 10:32:30 AM1/28/12
to mongod...@googlegroups.com
Can you send the output of printing r?

mayank gupta

unread,
Jan 28, 2012, 10:44:53 AM1/28/12
to mongod...@googlegroups.com
Output of r.toString()

{ _id: ObjectId('4f2269b8385db519a8e4b1f6'), rule: { condition: "{num:{$gt:20}}", continue: "false" } }

mayank gupta

unread,
Jan 28, 2012, 1:03:42 PM1/28/12
to mongod...@googlegroups.com
Another interesting observation is that when I cleaned and built the source again it ran correctly for the first time but gave the same error afterwards.

mayank gupta

unread,
Jan 30, 2012, 1:05:00 PM1/30/12
to mongod...@googlegroups.com
I figured it out. When i made a query to the db I didn't requested a getOwned() method from the buffered BSONObj .
This made the results inconsistent. Thanks for helping.
Reply all
Reply to author
Forward
0 new messages