Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB c++11 Driver get ID of inserted Document

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

like image 956
Cutton Eye Avatar asked Apr 02 '16 17:04

Cutton Eye


1 Answers

    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 =).

like image 146
Cutton Eye Avatar answered Nov 04 '22 15:11

Cutton Eye