美文网首页
01 mongodb C++11 驱动数组使用例子

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

作者: peimin | 来源:发表于2016-04-28 17:53 被阅读0次

官方例子地址

#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())

相关文章

网友评论

      本文标题:01 mongodb C++11 驱动数组使用例子

      本文链接:https://www.haomeiwen.com/subject/sushrttx.html