C++ MongoDB C++;连接示例分段错误
我正在尝试连接到我的云mongodb数据库。我能够使用MunGDB阿特拉斯访问数据库,但是使用C++连接是造成分段错误的。我想做的是创建一个mongodb实例,然后检查该实例是否存在C++ MongoDB C++;连接示例分段错误,c++,mongodb,cloud,C++,Mongodb,Cloud,我正在尝试连接到我的云mongodb数据库。我能够使用MunGDB阿特拉斯访问数据库,但是使用C++连接是造成分段错误的。我想做的是创建一个mongodb实例,然后检查该实例是否存在 #include <cstdlib> #include <iostream> #include <string> #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/builder/b
#include <cstdlib>
#include <iostream>
#include <string>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/stdx/make_unique.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/logger.hpp>
#include <mongocxx/options/client.hpp>
#include <mongocxx/uri.hpp>
namespace {
class logger final : public mongocxx::logger {
public:
explicit logger(std::ostream* stream) : _stream(stream) {}
void operator()(mongocxx::log_level level,
bsoncxx::stdx::string_view domain,
bsoncxx::stdx::string_view message) noexcept override {
if (level >= mongocxx::log_level::k_trace)
return;
*_stream << '[' << mongocxx::to_string(level) << '@' << domain << "] " << message << '\n';
}
private:
std::ostream* const _stream;
};
} // namespace
int main(int argc, char* argv[]) {
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
mongocxx::client conn{mongocxx::uri{}};
// The mongocxx::instance constructor and destructor initialize and shut down the driver,
// respectively. Therefore, a mongocxx::instance must be created before using the driver and
// must remain alive for as long as the driver is in use.
mongocxx::instance inst{bsoncxx::stdx::make_unique<logger>(&std::cout)};
try {
//const auto uri = mongocxx::uri{(argc >= 2) ? argv[1] : mongocxx::uri::k_default_uri};
const auto uri =mongocxx::uri{"mongodb+srv://user_name:passwd@test.qhkzc.mongodb.net/db_?"};
auto db = conn["name"];
{
// @begin: cpp-query-all
auto cursor = db["employees"].find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
// @end: cpp-query-all
}
return EXIT_SUCCESS;
} catch (const std::exception& xcp) {
std::cout << "connection failed: " << xcp.what() << "\n";
return EXIT_FAILURE;
}
}
但我可以运行以下代码:
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <iostream>
#include <chrono>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;
int main(void) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb+srv://USER:PASSWD@test.qhkzc.mongodb.net/DB?retryWrites=true&w=majority"}};
mongocxx::database db = conn["test"];
// Query for all the documents in a collection.
{
auto cursor = db["restaurants"].find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
// Replace the contents of a single document.
{
db["restaurants"].update_one(
make_document(kvp("name", "Vella")),
make_document(kvp("$set", make_document(kvp("cuisine", "American (New)"))),
kvp("$currentDate", make_document(kvp("lastModified", true)))));
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用bsoncxx::builder::basic::kvp;
使用bsoncxx::builder::basic::make\u数组;
使用bsoncxx::builder::basic::make\u文档;
内部主(空){
mongocxx::实例inst{};
mongocxx::客户端连接{mongocxx::uri{“mongodb”+srv://USER:PASSWD@test.qhkzc.mongodb.net/DB?retryWrites=true&w=maist“};
mongocxx::数据库db=conn[“test”];
//查询集合中的所有文档。
{
自动游标=db[“餐厅”]。查找({});
用于(自动和文档:光标){
std::您可以使用它来分析内核转储并获得崩溃的根本原因吗
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <iostream>
#include <chrono>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;
int main(void) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb+srv://USER:PASSWD@test.qhkzc.mongodb.net/DB?retryWrites=true&w=majority"}};
mongocxx::database db = conn["test"];
// Query for all the documents in a collection.
{
auto cursor = db["restaurants"].find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
// Replace the contents of a single document.
{
db["restaurants"].update_one(
make_document(kvp("name", "Vella")),
make_document(kvp("$set", make_document(kvp("cuisine", "American (New)"))),
kvp("$currentDate", make_document(kvp("lastModified", true)))));
}
}