gpt4 book ai didi

c++ - 在 Mongo CXX 中取消设置文档元素

转载 作者:行者123 更新时间:2023-11-30 04:46:27 27 4
gpt4 key购买 nike

我有一个类,它有一个 bsoncxx::document::view view 作为 private 属性,还有一个从 Mongo 数据库获取文档并保存的方法结果在本地 bsoncxx::document::value value 变量中,然后从 value.view() 中获取 View 并将其保存在类 bsoncxx 中::document::view View 变量:

const bool User::search_one_by_id(const std::string &id = "")
{
// The prototype of the method below: const std::tuple<bool, bsoncxx::document::value> search_one_by_id(mongocxx::collection &, const std::string &) const;
auto status = Crud::search_one_by_id(this->collection, id);

if (std::get<0>(status))
{
// Error
return EXIT_FAILURE;
}
else
{
// Success
bsoncxx::document::value value = std::get<1>(status);

bsoncxx::document::view view = value.view();

this->view = view;

return EXIT_SUCCESS;
}
}

问题是,如果我在上面的方法中从 View 中获取一些元素,即 return EXIT_SUCCESS 之前的下面的代码,则不会发出任何错误。

        bsoncxx::document::element element = this->view["first_name"];

std::cout << "First Name: " << element.get_utf8().value.to_string();

但是如果我得到一个 View ,将它保存在 bsoncxx::document::view view 变量中并尝试在另一个类方法中从 View 中获取一些元素:

void get_element()
{
bsoncxx::document::element element = this->view["first_name"];

std::cout << "First Name: " << element.get_utf8().value.to_string();
}

我收到错误:

terminate called after throwing an instance of 'bsoncxx::v_noabi::exception'
what(): unset document::element
Makefile:26: recipe for target 'run' failed
make: *** [run] Aborted (core dumped)

我曾尝试使用指针来保存对在 search_one_by_id 方法中获得的 View 的引用。我检查了我得到的属性类型 (first_name) 是否是获取元素值的正确类型。我检查了文档中是否存在该属性。我曾尝试使用 User::search_one_by_id 中的 view 中的 release() 方法。我通过以下方式检查 View 是否为空:

if (this->view.empty())
{
std::cout << "Empty: " << this->view.empty();
}
else
{
std::cout << "Loaded: " << this->view.empty() << " length " << this->view.length();
}

get_element方法中,输出为:

# If I comment the call to search_one_by_id
$ Empty: 1

# If I enable the call to search_one_by_id
$ Loaded: 0 length 129

backtrace 的 GDB 日志:

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff6fd7801 in __GI_abort () at abort.c:79
#2 0x00007ffff762c957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff7632ab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff7632af1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7632d24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff793985c in bsoncxx::v_noabi::document::element::type() const () from /usr/local/lib/libbsoncxx.so._noabi
#7 0x00007ffff7939abc in bsoncxx::v_noabi::document::element::get_utf8() const () from /usr/local/lib/libbsoncxx.so._noabi
#8 0x0000555555559353 in User::get_element() ()
#9 0x0000555555556668 in main ()

一些提示?该项目可以在 Github 上找到

引用资料:

Unset Document::element, MongoCXX Find Option Projection

Document value

Document view

Document element

document::element::operator[] overloads should not throw when given an invalid element

How do I get the type of a variable?

最佳答案

我认为您需要将指向值缓冲区的指针保存在某处。我想 View 对象指向已删除的内存。尝试这样的事情

class User
{
bsoncxx::document::value _value; <<< store at class level
};

const bool User::search_one_by_id(const std::string &id = "")
{
// The prototype of the method below: const std::tuple<bool, bsoncxx::document::value> search_one_by_id(mongocxx::collection &, const std::string &) const;
auto status = Crud::search_one_by_id(this->collection, id);

if (std::get<0>(status))
{
// Error
return EXIT_FAILURE;
}
else
{
// Success
_value = std::get<1>(status);
this->view = _value.view();

return EXIT_SUCCESS;
}
}

关于c++ - 在 Mongo CXX 中取消设置文档元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com