mongoDB c ++ 11驱动程序获取插入文档的ID

时间:2016-04-02 17:55:29

标签: mongodb c++11 get document mongo-cxx-driver

我正在使用新的c ++ 11 mongoDB驱动程序(不是传统驱动程序)。

我试图获得“身份证”。插入新文档后,mongoDB中的文档。此ID位于返回值" retVal3'。

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

这是没有auto命令的操作。我希望Eclipse能够解决这个问题并帮助我从中获取ID。没用。

调试时我可以看到ID。它保存在12字节数组中。以十六进制显示它显示ID。这个结构深入到这个结构中。

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

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

我不知道如何从这个struct / object中获取这12个字节。 它是一个对象吗?

是否存在功能? 你知道另一种方法吗?

THX

1 个答案:

答案 0 :(得分:5)

    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();

我问过mongoDB团队。得到了这个有效的回应=)。