Adding a BSON Array to a MongoDB 3.2 document and extracting the values back ( MongoCXX 3.2 ) ( C++ 11)

折月煮酒 提交于 2019-12-05 02:32:42

问题


    // The document I want to add data to and extract it back from c++
           bsoncxx::builder::stream::document data_builder,

           // I want to try and save this array in my document , as I want to  populate it later
           bsoncxx::builder::stream::array mybsonarr;
           for(float i = 0 ; i < 5 ; i = i + 0.1f){
             mybsonarr << i;
           }


// Now this line Throws an error 
data_builder << "_id" << 5 << "my_array" << &mybsonarr;

So how can I add my array and also how can I read back my float array to either and array or vector ?


回答1:


For adding array to stream document use open_array:

  using bsoncxx::builder::stream::document;
  using bsoncxx::builder::stream::open_array;
  using bsoncxx::builder::stream::close_array;
  using bsoncxx::builder::stream::finalize;

  document data_builder{};
  data_builder << "_id" << 5;
  auto array_builder = data_builder << "my_array" << open_array;
  for (float i = 0 ; i < 5 ; i = i + 0.1f) {
    array_builder << i;
  }
  array_builder << close_array;
  bsoncxx::document::value doc = data_builder << finalize;
  std::cout << bsoncxx::to_json(doc) << std::endl;


来源:https://stackoverflow.com/questions/36414387/adding-a-bson-array-to-a-mongodb-3-2-document-and-extracting-the-values-back-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!