c++ 向mongodb文档添加数组失败,前提条件失败:!(bson->标志& BSON_标志_输入_子项)

m1m5dgzv  于 2023-03-05  发布在  Go
关注(0)|答案(1)|浏览(134)

我尝试基于official documentation来做。下面是代码,我尝试向数组中添加一些项:

auto in_array = bsoncxx::builder::stream::document{}
        << "$set" << open_document << call_id_key << call_id << close_document << "$push"
        << open_document << transcription_key
        << open_array << open_document << direction_key << "INBOUND" << text_key 
        << transcript << confidence_key << confidence
        << start_time_key << start_time << end_time_key << end_time
        << "words" << open_array;

  for (const auto& word_info : words) {
    in_array = in_array << open_document << "word" << word_info.first <<
        "confidence" << word_info.second << close_document;  // Assertion happens here
  }

  bsoncxx::document::value doc = in_array << close_array << close_document << close_array << close_document << finalize;
  std::cout << bsoncxx::to_json(doc.view()) << std::endl;
eulz3vhy

eulz3vhy1#

我刚刚碰到了同样的问题,正如SPM所指出的,必须将文档创建与数组创建分开:

bsoncxx::builder::stream::document builder{};
auto in_array = builder << "$set" << open_document ....

相关问题