MongoDB在C++中的应用

莫兴言
2023-12-01

安装

参考链接: http://mongocxx.org/mongocxx-v3/installation/linux/

外部工具

$ sudo apt-get install libmongoc-1.0-0
$ sudo apt-get install libbson-1.0
$ sudo apt-get install cmake libssl-dev libsasl2-dev

安装mongo-c-driver

$ git clone -v https://github.com.cnpmjs.org/mongodb/mongo-c-driver.git
$ mkdir cmake-build
$ cd cmake-build
$ cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..
$ sudo make install

安装mongo-cxx-driver

$ git clone -v https://github.com.cnpmjs.org/mongodb/mongo-cxx-driver.git
$ mkdir cmake-build
$ cd cmake-build
$ sudo cmake ..                                \
    -DCMAKE_BUILD_TYPE=Release          \
    -DBSONCXX_POLY_USE_MNMLSTC=1                      \
    -DCMAKE_INSTALL_PREFIX=/usr/local
$ sudo make EP_mnmlstc_core
$ sudo make
$ sudo make install

代码

CMake工程配置文件

cmake_minimum_required(VERSION 2.8)

set(APPNAME demo)
project(${APPNAME})

set(ENV{PKG_CONFIG_PATH} /usr/lib/pkgconfig)
find_package(PkgConfig)
pkg_search_module(BSON REQUIRED libbsoncxx)
pkg_search_module(MONGO REQUIRED libmongocxx)

SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread -g -Wl,-rpath=./lib")

include_directories(app
    ${BSON_INCLUDE_DIRS}
    ${MONGO_INCLUDE_DIRS})

aux_source_directory(. DIRSRCS)
add_executable(${APPNAME}
    ${DIRSRCS}
    )

# link library to application
target_link_libraries(${APPNAME}
    ${BSON_LIBRARIES}
    ${MONGO_LIBRARIES}
    )

使用案例

#include "Mongo.h"

#include <glog/logging.h>

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/pool.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>

namespace mongo
{
    bool Mongo::Init()
    {
        LOG(INFO) << "mongo init...";

        try
        {
            mongocxx::instance instance{}; // This should be done only once.
            mongocxx::uri uri("mongodb://admin:123456@192.168.2.100:27017");

            mongocxx::pool pool{uri};
            auto client = pool.acquire();
            //列出数据库名称
            for(auto iter : client->list_database_names())
            {
                LOG(INFO)<<"database: "<<iter;
            }
            //获取数据库
            auto database = (*client)["mydb"];
            //列出集合
            for (auto iter : database.list_collection_names())
            {
                LOG(INFO)<<"collection name: "<<iter;
            }
            //获取集合
            auto collection = database["test1"];
            //查找集合数据
            mongocxx::cursor cursor = collection.find({});
            for(auto doc : cursor) 
            {
                LOG(INFO)<<"collection data: "<<bsoncxx::to_json(doc);
            }
            //删除集合
            //collection.drop();
            //查找集合数据
            auto find_doc = bsoncxx::builder::stream::document{} 
                << "name" << "John" << bsoncxx::builder::stream::finalize;
            bsoncxx::stdx::optional<bsoncxx::document::value> find_result = collection.find_one(find_doc);
            if(find_result) 
            {
                LOG(INFO)<<"find data: "<<bsoncxx::to_json(*find_result);
            }
            else
            {
                //插入数据
                collection.insert_one(bsoncxx::builder::stream::document{}<<"name"<<"John"
                    <<"age"<<18<<bsoncxx::builder::stream::finalize);
            }
        }
        catch(std::exception &e)
        {
            LOG(WARNING)<<"collection error: "<<e.what();
        }

        return true;
    }

    void Mongo::Release()
    {
        LOG(INFO) << "mongo release...";
    }
}
 类似资料: