samedi 2 avril 2016

Mongodb 3.2 C++11 drivers (Newest one) , Inserting array in document

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/array.hpp>

#include <bsoncxx/types.hpp>
#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

#include <mongocxx/options/update.hpp>

#include <iostream>

using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;



int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};

    auto db = conn["test2"];

    //bsoncxx::builder::stream::document document{};

    //auto collection = conn["testdb"]["testcollection"];
    //document << "hello" << "world";

    //collection.insert_one(document.view());
    //auto cursor = collection.find({});

    //for (auto&& doc : cursor) {
    //    std::cout << bsoncxx::to_json(doc) << std::endl;
    //}


   // I want to try and save this array in my document , as I want to populate it later
   bsoncxx::builder::stream::array mybsonarr;
    mybsonarr << 1.3f;
    mybsonarr << 2.3f;
    mybsonarr << 3.3f;





bsoncxx::builder::stream::document filter_builder, update_builder;

            filter_builder << "_id" << 5;

            update_builder << "$set" << open_document
               << "cuisine" << "Italian (New)"

               //For some-reason this works
               << "MyArray" << open_array <<

               [&](bsoncxx::builder::stream::array_context<> arr){
        for (float i = 0; i < 5; i = i + 0.1f)
            arr << i;
    } // Note that we don't invoke the lambda

               << close_array



               // But this does not --- why ????
               << "MYBSON Array" << open_array << &mybsonarr << close_array
               << close_document << "$currentDate" << open_document
               << "lastModified" << true << close_document;


    // We choose to move in our document here, which transfers ownership to insert_one()
  //  auto res = db["myRest"].update_one(std::move(restaurant_doc),std::move(restaurant_doc)).upsert(true);


    auto upsert = mongocxx::options::update();
    upsert.upsert(true);

    auto res = db["myRest2"].update_one(filter_builder.view(),update_builder.view(),upsert);

   //*/



    for (auto&& doc : db["myRest2"].find({})) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    std::cout <<" COUNT  :: "<< db["myRest2"].count({}) << std::endl ;
}

** Hello , I am trying to populate a mongodb bson::streaming::array and then i wan't to add to my mydocument , but it says some type when I try to add it in document in _core.hpp **

http://ift.tt/1V0fNNa

Aucun commentaire:

Enregistrer un commentaire