MongoDB C++11驱动程序如何获取插入文档的ID

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个字节。

这是一个对象吗?

是否存在相应的函数?或者你知道其他方法来获取它吗?

谢谢!


尝试像示例那样直接获取它失败了。 _generated_id 是私有的。 retVal3.val._generated_id._b_oid.value._bytes[]; 编译器:bsoncxx::v_noabi::types::value mongocxx::v_noabi::result::insert_one::_generated_id’ 是私有的。 - Cutton Eye
1个回答

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

我向mongoDB团队询问了此事,他们给出了可行的答复 =)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接