irpas技术客

C++调用MongoDB数据库_Dream谢榭_c++ mongodb

网络 5488

C++调用MongoDB数据库(一) 一、简介

本篇文章主要讲述如何使用C++调用MongoDB数据库实现增、删、查、改。

MongoDB

MongoDB是一个基于分布式文件存储的数据库,由C++语言编写,一般多用于WEB应用进行可扩展的高性能数据存储。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。

MongoDB文档

MongoDB中的记录是一个文档,它是由字段和值对组成的数据结构。 多个键及其关联的值有序地放在一起就构成了文档。 MongoDB文档类似于JSON对象。字段的值可以包括其他文档,数组和文档数组。

{ name: "xm", password: "123456", } MongoDB集合

MongoDB的集合就类似数据的表,数据库中的每行数据就是一个文档,及集合是一组文档的集合。

二、连接工具

C++本身并没有提供方法向连接MongoDB,但好在网上有C++连接MongoDB驱动(mongodb-c-driver ,mongo-cxx-driver),只是需要自行编译这些库的运行需要依赖boost库(对应编译方法及编译好的库,若有时间后面我会写文章介绍并给出编译好的库)

三、代码

这是通过工具进行MogoDB连接及增删查改的一些代码

#ifndef MONGODB_TOOL_OBJ_H #define MONGODB_TOOL_OBJ_H #include <bsoncxx/builder/stream/document.hpp> #include <bsoncxx/types.hpp> #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/uri.hpp> #include <bsoncxx/json.hpp> #include <QString> #include <QVariantMap> using bsoncxx::builder::stream::document; 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; using namespace mongocxx; using namespace mongocxx::options; using bsoncxx::builder::basic::kvp; using bsoncxx::builder::basic::make_document; using bsoncxx::to_json; class MongoDBToolObj { protected: mongocxx::instance* m_dbInstance = nullptr; mongocxx::client* m_client = nullptr; QString m_hostName; QString m_port; public: stdx::optional<bsoncxx::document::value> find_One(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::find& options = mongocxx::v_noabi::options::find()); cursor* find(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::find& options = mongocxx::v_noabi::options::find()); stdx::optional<result::insert_one> insert_One(QString db, QString coll, document& doc, const mongocxx::v_noabi::options::insert& options = {}); stdx::optional<result::update> update_One(QString db, QString coll, document& filter, document& value, const mongocxx::v_noabi::options::update& options = mongocxx::v_noabi::options::update()); stdx::optional<result::update> update_Many(QString db, QString coll, document& filter, document& value, const mongocxx::v_noabi::options::update& options = mongocxx::v_noabi::options::update()); stdx::optional<result::delete_result> delete_One(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::delete_options& options = mongocxx::v_noabi::options::delete_options()); stdx::optional<result::delete_result> delete_Many(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::delete_options& options = mongocxx::v_noabi::options::delete_options()); std::int64_t countDocument(QString db, QString coll, document& filter, const options::count& option = options::count()); public: MongoDBToolObj(); MongoDBToolObj(QString hostName, QString port); void setHostName(QString hostName); void setPort(QString port); bool connectToHost(); mongocxx::client* client(); ~MongoDBToolObj(); public: stdx::optional<bsoncxx::builder::stream::document> mapToDocument(QMap<QString, QString> map); }; #endif // MONGODB_TOOL_OBJ_H #include "MongoDBToolObj.h" MongoDBToolObj::MongoDBToolObj() { } MongoDBToolObj::MongoDBToolObj(QString hostName, QString port) { m_hostName = hostName; m_port = port; } void MongoDBToolObj::setHostName(QString hostName) { m_hostName = hostName; } void MongoDBToolObj::setPort(QString port) { m_port = port; } bool MongoDBToolObj::connectToHost() { if ((m_hostName != "") && (m_port != "")) { if (m_dbInstance) { delete m_dbInstance; m_dbInstance = nullptr; } if (m_client) { delete m_client; m_client = nullptr; } m_dbInstance = new(std::nothrow) mongocxx::instance(); m_client = new(std::nothrow) mongocxx::client(mongocxx::uri{ ("mongodb://" + m_hostName + ":" + m_port).toStdString().c_str() }); if (!m_client) { return false; } else { return true; } } else { return false; } } mongocxx::client* MongoDBToolObj::client() { return m_client; } stdx::optional<bsoncxx::document::value> MongoDBToolObj::find_One(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::find& options) { stdx::optional<bsoncxx::document::value> ret; if (m_dbInstance && m_client) { return (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].find_one(filter.view(), options); } return ret; } cursor* MongoDBToolObj::find(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::find& options) { if (m_dbInstance && m_client) { auto c = (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].find(filter.view(), options); return new(std::nothrow) cursor(std::move(c)); } return nullptr; } stdx::optional<result::insert_one> MongoDBToolObj::insert_One(QString db, QString coll, document& doc, const mongocxx::v_noabi::options::insert& options) { stdx::optional<result::insert_one> ret; if (m_dbInstance && m_client) { ret = (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].insert_one(doc.view(), options); } return ret; } stdx::optional<result::update> MongoDBToolObj::update_One(QString db, QString coll, document& filter, document& value, const mongocxx::v_noabi::options::update& options) { stdx::optional<result::update> ret; if (m_dbInstance && m_client) { ret = (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].update_one( filter.view(), make_document(kvp("$set", value)), options ); } return ret; } stdx::optional<result::update> MongoDBToolObj::update_Many(QString db, QString coll, document& filter, document& value, const mongocxx::v_noabi::options::update& options) { stdx::optional<result::update> ret; if (m_dbInstance && m_client) { ret = (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].update_many( filter.view(), make_document(kvp("$set", value)), options ); } return ret; } stdx::optional<result::delete_result> MongoDBToolObj::delete_One(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::delete_options& options) { stdx::optional<result::delete_result> ret; if (m_dbInstance && m_client) { ret = (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].delete_one(filter.view(), options); } return ret; } stdx::optional<result::delete_result> MongoDBToolObj::delete_Many(QString db, QString coll, document& filter, const mongocxx::v_noabi::options::delete_options& options) { stdx::optional<result::delete_result> ret; if (m_dbInstance && m_client) { ret = (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].delete_many(filter.view(), options); } return ret; } std::int64_t MongoDBToolObj::countDocument(QString db, QString coll, document& filter, const options::count& option) { return (*m_client)[db.toStdString().c_str()][coll.toStdString().c_str()].count_documents(filter.view(), option); } MongoDBToolObj::~MongoDBToolObj() { delete m_dbInstance; delete m_client; } stdx::optional<bsoncxx::builder::stream::document> MongoDBToolObj::mapToDocument(QMap<QString,QString> map) { bsoncxx::builder::stream::document doc; QMap<QString,QString>::iterator iter = map.begin(); while (iter != map.end()) { // qDebug() << "Iterator " << iter.key() << ":" << iter.value(); // 迭代器 doc << iter.key().toStdString() << iter.value().toStdString(); iter++; } return doc; }


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #C #mongodb