6

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

插入新文档后,我试图在 mongoDB 中获取文档的“id”。此 ID 在返回值“retVal3”中。

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

这是没有自动命令的操作。我希望 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]

我不知道如何从这个结构/对象中取出这 12 个字节。它是一个对象吗?

是否存在现有的功能?你知道另一种方法来把它弄出来吗?

谢谢

4

1 回答 1

7
    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 团队。得到了这个工作响应=)。

于 2016-04-06T23:41:59.160 回答