将BSON数组添加到MongoDB 3.2文档中,然后取回值(MongoCXX 3.2)(C ++ 11) [英] Adding a BSON Array to a MongoDB 3.2 document and extracting the values back ( MongoCXX 3.2 ) ( C++ 11)

查看:200
本文介绍了将BSON数组添加到MongoDB 3.2文档中,然后取回值(MongoCXX 3.2)(C ++ 11)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    // 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;

那么我该如何添加数组,又如何将float数组读回array和vector呢?

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

推荐答案

要在流文档中添加数组,请使用open_array:

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;

这篇关于将BSON数组添加到MongoDB 3.2文档中,然后取回值(MongoCXX 3.2)(C ++ 11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆