问题描述
我使用以下链接编译了mongocxx 3.6:
http://mongocxx.org/mongocxx-v3/installation/#configuring-with-mongocxx-3-5-0-or-newer
Please find the below :
#include <iostream>
#include <sstream>
#include <thread>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/types.hpp>
#include <mongocxx/exception/exception.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/pool.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;
using mongocxx::collection;
class MongoDBServer{
mongocxx::instance instance_;
mongocxx::uri uri_;
mongocxx::pool pool_;
public:
MongoDBServer():
instance_{},uri_{},pool_{uri_} {
std::cout << "MONGO INIT\n";
}
public:
int mongocxx_insert_data(){
std::vector<std::thread> threads{};
for (auto i : {0}) {
auto run = [&](std::int64_t j) {
auto client = pool_.acquire();
auto coll = (*client)["geo_db"]["loc_history"];
coll.delete_many({});
bsoncxx::document::value loc_doc = make_document(
kvp("type","GeoJSON"),kvp("mac","94:9F:A7:3D:AD:DB"),kvp("location",make_document(kvp("coordinates",make_array(
make_document(kvp("dir","10,00"),kvp("timestamp",bsoncxx::types::b_date{std::chrono::seconds{1604233286}}),kvp("speed","10.000"),kvp("coord",make_array(25.8557413,55.7820266))),make_document(kvp("dir",bsoncxx::types::b_date{std::chrono::seconds{1604233595}}),make_array(25.9557413,55.7720266))))))),kvp("log_start_time",bsoncxx::types::b_date{std::chrono::milliseconds{12323}}),kvp("log_end_time",bsoncxx::types::b_date{std::chrono::milliseconds{121212}}));
try {
auto result = coll.insert_one(std::move(loc_doc));
if (!result) {
std::cout << "UnackNowledged write. No id available." << std::endl;
return EXIT_SUCCESS;
}
if (result->inserted_id().type() == bsoncxx::type::k_oid) {
bsoncxx::oid id = result->inserted_id().get_oid().value;
std::string id_str = id.to_string();
std::cout << "Inserted id: " << id_str << std::endl;
} else {
std::cout << "Inserted id was not an OID type" << std::endl;
}
} catch (const mongocxx::exception& e) {
std::cout << "An exception occurred: " << e.what() << std::endl;
return EXIT_FAILURE;
}
};
std::thread runner{run,i};
threads.push_back(std::move(runner));
}
for (auto&& runner : threads) {
runner.join();
}
}
};
int main(int,char**) {
MongoDBServer mongodbserver_;
mongodbserver_.mongocxx_insert_data();
std::cout << "INSERTED SUCCESSFULLY\n";
}
使用上面的代码: 如果我用命令编译
c++ --std=c++11 geopoolclass.cpp -o geodata_pool $(pkg-config --cflags --libs libmongocxx)
MongoDB正常工作。
但是如果我用
进行编译c++ --std=c++17 geopoolclass.cpp -o geodata_pool $(pkg-config --cflags --libs libmongocxx)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)