gpt4 book ai didi

mongodb - 取消设置文档::元素,MongoCXX 查找选项投影

转载 作者:可可西里 更新时间:2023-11-01 10:42:27 27 4
gpt4 key购买 nike

以下是尝试使用带有项目查找选项的 mongo 查询的代码。

    using bsoncxx::builder::stream::document;
mongocxx::options::find opts;
document condition, options;

const static int readdomain = 90000;

condition << "lastRead" << open_document << "$gte" << readdomain << close_document;
options << "read" << 1;
opts.projection(options.view());
mongocxx::cursor cursor = collection.find(condition.view(), opts);

没有“opts”的上述查询可以很好地工作,而带有“opts”的查询将引发以下错误。

terminate called after throwing an instance of 'bsoncxx::v_noabi::exception'
what(): unset document::element
[zy1989:20594] *** Process received signal ***
[zy1989:20594] Signal: Aborted (6)
[zy1989:20594] Signal code: (-6)
[zy1989:20594] [ 0] /lib/x86_64-linux-gnu/libpthread.so.0(+0x10d10)[0x7f05d3b65d10]
[zy1989:20594] [ 1] /lib/x86_64-linux-gnu/libc.so.6(gsignal+0x37)[0x7f05d37c0267]
[zy1989:20594] [ 2] /lib/x86_64-linux-gnu/libc.so.6(abort+0x16a)[0x7f05d37c1eca]
[zy1989:20594] [ 3] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(_ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x16d)[0x7f05d471106d]
[zy1989:20594] [ 4] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x5eee6)[0x7f05d470eee6]
[zy1989:20594] [ 5] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x5ef31)[0x7f05d470ef31]
[zy1989:20594] [ 6] /usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x5f149)[0x7f05d470f149]
[zy1989:20594] [ 7] /usr/local/lib/libbsoncxx.so._noabi(_ZNK7bsoncxx7v_noabi8document7element4typeEv+0xdd)[0x7f05d49cfe0d]
[zy1989:20594] [ 8] /usr/local/lib/libbsoncxx.so._noabi(_ZN7bsoncxx7v_noabi7to_jsonENS0_8document7elementE+0x286)[0x7f05d49d8926]
[zy1989:20594] [ 9] ./ArticleIndexComputation(_Z7groupbyN8mongocxx7v_noabi6cursor8iteratorElRSt6vectorIN7bsoncxx7v_noabi8document4viewESaIS7_EERSt3mapIiS3_IiSaIiEESt4lessIiESaISt4pairIKiSD_EEE+0x186)[0x415826]
[zy1989:20594] [10] ./ArticleIndexComputation(main+0x1181)[0x417161]
[zy1989:20594] [11] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f05d37aba40]
[zy1989:20594] [12] ./ArticleIndexComputation(_start+0x29)[0x415439]
[zy1989:20594] *** End of error message ***

最佳答案

当我尝试从完全没有这些键的文档中获取键值时,它会引发 [what(): unset document::element]。

    using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;

mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://10.9.0.1:27017"}};
mongocxx::collection collection = conn["wechat"]["article"];

mongocxx::options::find opts;
mongocxx::options::count copts;
document condition, options;

condition << "$and" << open_array <<
open_document << "subscribeRobot" << open_document << "$exists" << 1 << close_document << close_document <<
open_document << "body.title" << open_document << "$exists" << 1 << close_document << close_document <<
open_document << "body.content" << open_document << "$exists" << 1 << close_document << close_document <<
close_array;

options << "subscribeRobot" << 1 << "body.title" << 1 << "body.content" << 1;
opts.projection(options.view());
copts.limit(1000);

mongocxx::cursor cursor = collection.find(condition.view(), opts);
dociter docbegin = cursor.begin();
int64_t allartnumber = collection.count(condition.view(), copts);
cout << allartnumber << endl;

# pragma omp parallel for
for(int i = 0; i < allartnumber; i++){
doc = *(next(docbegin, i));
wechat = bsoncxx::to_json(doc["subscribeRobot"]);
title = bsoncxx::to_json(doc["body"]["title"]);
// It will raise [what(): unset document::element] when these key above don't exists.
}

关于mongodb - 取消设置文档::元素,MongoCXX 查找选项投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35886398/

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