#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())
网友评论