mongocxx 如何从视图中构建文档?
mongocxx how to build a document from a view?
auto cursor = db["friend"].find({});
for (auto &&docView : cursor) {
bsoncxx::builder::basic::document document1;
document1.append(docView); // This line will be an error
document1.append(kvp("surl", "http://xxx"));
document1.append(kvp("burl", "http://xxx"));
arr.append(document1);
}
我想创建一个新文档,包含查询结果,并添加一些新字段到document.But上面的代码不能运行。谁能告诉我怎么办,谢谢!
查询结果docView是这样的:
{
"name": "BeJson",
"headUrl": "http://www.bejson.com"
}
我想像这样构建 document1:
{
"name": "BeJson",
"headUrl": "http://www.bejson.com",
"surl": "http://xxx",
"burl": "http://xxx"
}
我想,如果我理解你的问题,你应该使用 builder::concatenate
:
auto cursor = db["friend"].find({});
for (auto &&docView : cursor) {
bsoncxx::builder::basic::document document1;
document1.append(bsoncxx::builder::concatenate(docView));
document1.append(kvp("surl", "http://xxx"));
document1.append(kvp("burl", "http://xxx"));
}
auto cursor = db["friend"].find({});
for (auto &&docView : cursor) {
bsoncxx::builder::basic::document document1;
document1.append(docView); // This line will be an error
document1.append(kvp("surl", "http://xxx"));
document1.append(kvp("burl", "http://xxx"));
arr.append(document1);
}
我想创建一个新文档,包含查询结果,并添加一些新字段到document.But上面的代码不能运行。谁能告诉我怎么办,谢谢!
查询结果docView是这样的:
{
"name": "BeJson",
"headUrl": "http://www.bejson.com"
}
我想像这样构建 document1:
{
"name": "BeJson",
"headUrl": "http://www.bejson.com",
"surl": "http://xxx",
"burl": "http://xxx"
}
我想,如果我理解你的问题,你应该使用 builder::concatenate
:
auto cursor = db["friend"].find({});
for (auto &&docView : cursor) {
bsoncxx::builder::basic::document document1;
document1.append(bsoncxx::builder::concatenate(docView));
document1.append(kvp("surl", "http://xxx"));
document1.append(kvp("burl", "http://xxx"));
}