6

I'm working with new c++11 mongoDB driver ( NOT the Legacy Driver ).

I'm trying to get the 'id' of a document in mongoDB after I inserted a new document. This ID is in the returned value 'retVal3'.

    struct core::v1::optional<mongocxx::v_noabi::result::insert_one> retVal3 = collection.insert_one(document.view());

This is the operation with out the auto command. I hoped Eclipse would be able to resolve this and help me to get the ID out of it. Did not work.

While debugging I can see the ID. Its saved in a 12 Byte array. Displaying in hex it shows the ID. This arry is deep deep down in this struct.

retVal3 ==> core::v1::impl::storage<mongocxx::v_noabi::result::insert_one, false> ==>

val ==> _generated_id ==> _b_oid ==> value ==> _bytes ==> _M_elems char [12]

I do have no idea how to get those 12 Bytes out of this struct/object. Is it a object?

Is there a Function, which is existing? Do you know a other way to get it out?

Thx

acm
  • 12,183
  • 5
  • 39
  • 68
Cutton Eye
  • 3,207
  • 3
  • 20
  • 39
  • The attemt do get directialy to it like the example faild. `_generated_id` is private. `retVal3.val._generated_id._b_oid.value._bytes[];` compiler: bsoncxx::v_noabi::types::value mongocxx::v_noabi::result::insert_one::_generated_id’ is private – Cutton Eye Apr 03 '16 at 11:19

1 Answers1

8
    auto retVal = db.insert_one(hey.view());  // Part where document is uploaded to database
    bsoncxx::oid oid = retVal->inserted_id().get_oid().value;
    std::string JobID = oid.to_string();

I asked the mongoDB team. got this working response =).

Cutton Eye
  • 3,207
  • 3
  • 20
  • 39