c++ - 在 Mongo CXX 中取消设置文档元素
问题描述
我有一个具有 absoncxx::document::view view
作为private
属性的类,以及一个从 Mongo 数据库中获取文档并将结果保存在局部bsoncxx::document::value value
变量中然后从 中获取视图value.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;
}
}
问题是,如果我从上述方法中的视图中获取一些元素,即下面的代码之前的代码return EXIT_SUCCESS
,则不会发出错误。
bsoncxx::document::element element = this->view["first_name"];
std::cout << "First Name: " << element.get_utf8().value.to_string();
但是,如果我得到一个视图,请将其保存在bsoncxx::document::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
方法中获得的视图的引用。我检查了我得到的属性 ( first_name
) 的类型是否是获取元素值的正确类型。我检查了文档中是否存在该属性。我曾尝试release()
使用view
. User::search_one_by_id
我通过以下方式检查视图是否为空:
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
GDB 日志到backtrace
:
#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
解决方案
我认为您需要将指向缓冲区的指针保存在某处。我想视图对象指向已删除的内存。尝试这样的事情
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;
}
}
推荐阅读
- function - Kotlin 使用数据类发送函数参数
- mysql - 如何避免作为 sql 查询输出的一部分返回的字符串值被拆分为 bash/shell 脚本中数组中的不同字段
- pandas - pandas-read-xml 在“json-normalize”上有错误
- amazon-web-services - 如何将新客户端添加到基于相互证书的 AWS 客户端 VPN?
- typescript - 使用 EventSubscriber 在 mikroorm 中注册数据库历史记录
- java - jaxrs 覆盖 ContextResolver 提供程序
- spotify - 从 Spotify API 吸引关注者数量
- dismo - rstudio 请求旧版本的 java 但 macbook 不允许安装它
- python - Django模型的message_set
- c# - 我的代码有问题还是 Visual Studio 代码损坏