MongoDB在C++中的应用

安装

参考链接: 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...";
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值