gpt4 book ai didi

c++ - 将 BSON 数组添加到 MongoDB 3.2 文档并将值提取回来 (MongoCXX 3.2) (C++ 11)

转载 作者:可可西里 更新时间:2023-11-01 09:35:12 25 4
gpt4 key购买 nike

    // 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 组读回数组或 vector ?

最佳答案

要将数组添加到流文档,请使用 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;

关于c++ - 将 BSON 数组添加到 MongoDB 3.2 文档并将值提取回来 (MongoCXX 3.2) (C++ 11),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com