vendredi 24 septembre 2021

Conversion Error doing bulk_write in mogocxx driver

I'm trying to learn to use c++ driver for mongodb. I am already familiar with pymongo usage. This is what i'm trying to do: read all docs from a collection, append it to an array, process the data, and then do a bulk replace operation, which is basically writing the same docs again. For now, I'm ignoring the process part and only doing the mongo to array and array to bulk replace.

Here's the code:

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/array.hpp>


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

using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;


int main(){
    mongocxx::instance instance{}; // This should be done only once.
    mongocxx::client client{mongocxx::uri{}};
    mongocxx::database db = client["mydb"];
    mongocxx::collection coll = db["mycollection"];

    mongocxx::cursor cursor = coll.find({});

    std::vector<bsoncxx::document::view> docs;
    for(auto doc : cursor) {
        docs.push_back(doc);
    }

    std::cout<<docs.size() << std::endl;

    auto bulk = coll.create_bulk_write();

    for (auto doc : docs){
        auto temp = make_document(kvp("_id", doc["_id"]));
        mongocxx::model::replace_one replace_op{temp.view(), doc};
        bulk.append(replace_op);
    }
    bulk.execute();
}

I keep getting this following error while compiling :


In file included from /usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/array.hpp:19:0,
                 from /usr/local/include/mongocxx/v_noabi/mongocxx/collection.hpp:18,
                 from /usr/local/include/mongocxx/v_noabi/mongocxx/database.hpp:23,
                 from /usr/local/include/mongocxx/v_noabi/mongocxx/client.hpp:20,
                 from mongotest.cpp:5:
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/impl.hpp: In instantiation of 'typename std::enable_if<((! std::is_convertible<T, std::function<void(bsoncxx::v_noabi::builder::basic::sub_document)> >::value) && (! std::is_convertible<T, std::function<void(bsoncxx::v_noabi::builder::basic::sub_array)> >::value)), void>::type bsoncxx::v_noabi::builder::basic::impl::generic_append(bsoncxx::v_noabi::builder::core*, T&&) [with T = bsoncxx::v_noabi::document::element; typename std::enable_if<((! std::is_convertible<T, std::function<void(bsoncxx::v_noabi::builder::basic::sub_document)> >::value) && (! std::is_convertible<T, std::function<void(bsoncxx::v_noabi::builder::basic::sub_array)> >::value)), void>::type = void]':
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/impl.hpp:60:19:   required from 'void bsoncxx::v_noabi::builder::basic::impl::value_append(bsoncxx::v_noabi::builder::core*, T&&) [with T = bsoncxx::v_noabi::document::element]'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/sub_document.hpp:86:27:   required from 'void bsoncxx::v_noabi::builder::basic::sub_document::append_(std::tuple<const char (&)[n], V>&&) [with long unsigned int n = 4; V = bsoncxx::v_noabi::document::element&&]'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/sub_document.hpp:47:9:   required from 'void bsoncxx::v_noabi::builder::basic::sub_document::append(Arg&&, Args&& ...) [with Arg = std::tuple<const char (&)[4], bsoncxx::v_noabi::document::element&&>; Args = {}]'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/document.hpp:112:5:   required from 'bsoncxx::v_noabi::document::value bsoncxx::v_noabi::builder::basic::make_document(Args&& ...) [with Args = {std::tuple<const char (&)[4], bsoncxx::v_noabi::document::element&&>}]'
mongotest.cpp:43:57:   required from here
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/impl.hpp:55:5: error: no matching function for call to 'bsoncxx::v_noabi::builder::core::append(bsoncxx::v_noabi::document::element)'
     core->append(std::forward<T>(t));
     ^~~~
In file included from /usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/sub_array.hpp:19:0,
                 from /usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/impl.hpp:17,
                 from /usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/basic/array.hpp:19,
                 from /usr/local/include/mongocxx/v_noabi/mongocxx/collection.hpp:18,
                 from /usr/local/include/mongocxx/v_noabi/mongocxx/database.hpp:23,
                 from /usr/local/include/mongocxx/v_noabi/mongocxx/client.hpp:20,
                 from mongotest.cpp:5:
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:175:11: note: candidate: bsoncxx::v_noabi::builder::core& bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_double&)
     core& append(const types::b_double& value);
           ^~~~~~
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:175:11: note:   no known conversion for argument 1 from 'bsoncxx::v_noabi::document::element' to 'const bsoncxx::v_noabi::types::b_double&'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:189:11: note: candidate: bsoncxx::v_noabi::builder::core& bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_utf8&)
     core& append(const types::b_utf8& value);
           ^~~~~~
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:189:11: note:   no known conversion for argument 1 from 'bsoncxx::v_noabi::document::element' to 'const bsoncxx::v_noabi::types::b_utf8&'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:203:11: note: candidate: bsoncxx::v_noabi::builder::core& bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_document&)
     core& append(const types::b_document& value);
           ^~~~~~
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:203:11: note:   no known conversion for argument 1 from 'bsoncxx::v_noabi::document::element' to 'const bsoncxx::v_noabi::types::b_document&'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:217:11: note: candidate: bsoncxx::v_noabi::builder::core& bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_array&)
     core& append(const types::b_array& value);
           ^~~~~~
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:217:11: note:   no known conversion for argument 1 from 'bsoncxx::v_noabi::document::element' to 'const bsoncxx::v_noabi::types::b_array&'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:231:11: note: candidate: bsoncxx::v_noabi::builder::core& bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_binary&)

I'm able to do the same thing in python like so:

docs = []
for doc in conn['mycoll'].find({}):
    docs.append(doc)
    
rep = []
for doc in docs:
    rep.append(ReplaceOne({'_id': doc['_id']}, doc))

conn['mycoll'].bulk_write(rep)

I want to understand what's going on and what i'm doing wrong here.

Aucun commentaire:

Enregistrer un commentaire