I am using the mongo-cxx 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 I get the following error:
BSONElement: bad type #somenumber
The query object returns a pointer to the buffer where the
BSONObjis stored. I went wrong when I did not use the functionBSONObj::getOwned()to request a copy of theBSONObjwhich was causing the exception.