2016-04-04 13 views
3
// 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 wie kann ich hinzufügen meine Array zu extrahieren und auch, wie kann ich lesen Sie meine Float-Array entweder und Array zurück oder Vektor?Hinzufügen eines BSON Array zu einem MongoDB 3.2 Dokument und die Werte zurück (MongoCXX 3.2) (C++ 11)

Antwort

3

Bei Array Hinzufügen open_array Dokument Gebrauch stream:

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;