mongodb c ,未能添加一个数组

MongoDB C++, failed to add an array to document

本文关键字:一个 数组 添加 mongodb      更新时间:2023-10-16

在将数组添加到文档时,open_array和colles_array必须同时扔入文档流。添加" CLOSS_ARRAY"时,后续代码在最后一行失败(编译时间)。

vector<string> List;
...
document Doc;
Doc <<"List" <<open_array;
for (string Str: List) {
  Doc <<Str;
}
Doc <<close_array;

,但我不知道"列表"中要添加到文档的元素数量。MongoDB仍然缺少C 驱动程序的示例。

此代码有效,但"列表"中的项目数不知道。

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.

找到了将BSON阵列添加到MongoDB 3.2文档中并将值提取的解决方案(Mongocxx 3.2)(C 11)

它无法在文档本身中添加'Close_Array',必须通过" Array Builder"(键入'auto''添加它,我没有将其挖出来找到实际类型)。

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;
相关文章: