01 mongodb C++11 驱动数组使用例子

官方例子地址

#include <bsoncxx/builder/stream/array.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/config/prelude.hpp>
#include <bsoncxx/types.hpp>

struct student
{
    int id;
    int score;
};

void set(bsoncxx::builder::stream::document& doc, const std::vector<student>& students)
{
    NewDoc << "role_id" << RoleId
        << "sign" << bsoncxx::builder::stream::open_array
        << [&](bsoncxx::builder::stream::array_context<> arr) {
        for (int i = 0; i < NumOfDays; i++)
        {
            arr << bsoncxx::builder::stream::open_document
                << "day" << 1
                << "issign" << false
                << bsoncxx::builder::stream::close_document;
        }
    } << bsoncxx::builder::stream::close_array;
}

void get(const bsoncxx::document::view& View, std::vector<student>& students)
{
    auto studs = View["students"].get_array();
    for (auto& Item : studs.value)
    {
        student stu;
        stu.id    = Item["id"];
        stu.score = Item["score"];
        students.push_back(stu);
    }
} 

今天发现存一个空的数组会有问题在读取的时候:

    "sign" : [ 
        {}
    ]

后来改成空的时候不存 用的时候判断一下

    auto iter = View.find("sign");
    if (iter != View.end())
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,911评论 25 709
  • 就像所有的节日一样,农历的正月初一充满了节日的气氛。在老家,正月初一事儿好多。小小年纪很期待。 穿新衣 正月初一,...
    wwling2001阅读 2,817评论 0 1
  • 世界上有一家奇怪的银行,他给每个人都开了账户,每天都给你账户上存入同样数目的资金,必须当天用完,不准有余额记账,不...
    带起微笑的琨琨阅读 2,662评论 2 2
  • 我想取个副标题,竟然不知道写什么。 这个故事据说是萧红怀念小时候,怀念祖父而写。 老主人死了,小主人逃难去了。 实...
    如止匕卑微阅读 3,602评论 0 1

友情链接更多精彩内容