向文档添加数组时,open_array和close_array必须在投入文档流时同时出现。添加' close_array'时,跟随代码在最后一行失败(编译时)。
vector<string> List;
...
document Doc;
Doc <<"List" <<open_array;
for (string Str: List) {
Doc <<Str;
}
Doc <<close_array;
但我不知道&#39; List&#39;中的元素数量。同时添加到文档。 MongoDB仍缺少C ++驱动程序的示例。
此代码有效,但&#39;列表中的项目数量为&#39;不知道。
Doc
<<open_array
<<List[0] <<List[1] <<List[2] <<...
<<close_array;
G ++错误:
content.cpp:65:7: error: no match for ‘operator<<’ (operand types are ‘bsoncxx::v_noabi::builder::stream::document’ and ‘const bsoncxx::v_noabi::builder::stream::close_array_type’)
Doc <<close_array;
~~~~^~~~~~~~~~~~~
compilation terminated due to -Wfatal-errors.
答案 0 :(得分:0)
无法添加&#39; close_array&#39;对于文档本身,它必须通过&#39;添加。一个数组构建器(键入&#39; auto&#39;,我没有挖出来找到真正的类型)。
auto Array = Doc <<"List" <<open_array;
for (string Str: List)
Array <<Str;
Array <<close_array;
需要注意的是,上面的代码运行正常,但以下内容不会
auto Array = Doc <<"List";
Array <<open_array;
for (string Str: List)
Array <<Str;
Array <<close_array;