首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Mongo CXX中取消设置文档元素

在Mongo CXX中取消设置文档元素
EN

Stack Overflow用户
提问于 2019-06-25 00:36:46
回答 1查看 420关注 0票数 1

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

代码语言:javascript
运行
复制
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之前的下面的代码,就不会发出错误。

代码语言:javascript
运行
复制
        bsoncxx::document::element element = this->view["first_name"];

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

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

代码语言:javascript
运行
复制
void get_element()
{
    bsoncxx::document::element element = this->view["first_name"];

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

我收到以下错误:

代码语言:javascript
运行
复制
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)的类型是否是获取元素值的正确类型。我已经检查了文档中是否存在该属性。我曾尝试在User::search_one_by_id中使用view中的release()方法。我通过以下命令检查视图是否为空:

代码语言:javascript
运行
复制
if (this->view.empty())
{
    std::cout << "Empty: " << this->view.empty();
}
else
{
    std::cout << "Loaded: " << this->view.empty() << " length " << this->view.length();
}

get_element方法中,输出是:

代码语言:javascript
运行
复制
# 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

代码语言:javascript
运行
复制
#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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-25 01:49:15

我认为你需要把指向有值的缓冲区的指针放在某个地方。我想视图对象指向已删除的内存。试试这样的东西

代码语言:javascript
运行
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56740730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档