#c #mongodb #c 11 #mongodb-query
Вопрос:
Я пытаюсь научиться использовать драйвер c для mongodb. Я уже знаком с использованием pymongo. Это то, что я пытаюсь сделать: прочитать все документы из коллекции, добавить их в массив, обработать данные, а затем выполнить операцию массовой замены, которая в основном заключается в повторном написании тех же документов. На данный момент я игнорирую часть процесса и выполняю только монго для массива и массив для массовой замены.
Вот код:
#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();
}
Я продолжаю получать следующую ошибку при компиляции :
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) amp;amp; (! 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*, Tamp;amp;) [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) amp;amp; (! 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*, Tamp;amp;) [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 (amp;)[n], V>amp;amp;) [with long unsigned int n = 4; V = bsoncxx::v_noabi::document::elementamp;amp;]'
/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(Argamp;amp;, Argsamp;amp; ...) [with Arg = std::tuple<const char (amp;)[4], bsoncxx::v_noabi::document::elementamp;amp;>; 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(Argsamp;amp; ...) [with Args = {std::tuple<const char (amp;)[4], bsoncxx::v_noabi::document::elementamp;amp;>}]'
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::coreamp; bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_doubleamp;)
coreamp; append(const types::b_doubleamp; 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_doubleamp;'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:189:11: note: candidate: bsoncxx::v_noabi::builder::coreamp; bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_utf8amp;)
coreamp; append(const types::b_utf8amp; 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_utf8amp;'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:203:11: note: candidate: bsoncxx::v_noabi::builder::coreamp; bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_documentamp;)
coreamp; append(const types::b_documentamp; 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_documentamp;'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:217:11: note: candidate: bsoncxx::v_noabi::builder::coreamp; bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_arrayamp;)
coreamp; append(const types::b_arrayamp; 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_arrayamp;'
/usr/local/include/bsoncxx/v_noabi/bsoncxx/builder/core.hpp:231:11: note: candidate: bsoncxx::v_noabi::builder::coreamp; bsoncxx::v_noabi::builder::core::append(const bsoncxx::v_noabi::types::b_binaryamp;)
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)
Я хочу понять, что происходит и что я здесь делаю не так.