IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> windows和Linux下mongocxx编译及使用说明 -> 正文阅读

[系统运维]windows和Linux下mongocxx编译及使用说明

windows和Linux下mongocxx编译及使用说明

  • 基于mongo-c-driver-1.13.0和mongo-cxx-driver-r3.4.0,地址分别为:mongo-cxx-drivermongo-c-driver
  • windows使用visual studio 2017编译release库,依赖boost1.66
  • Linux使用系统自带的gcc和boost版本库,在ubuntu16.04编译成功

Linux编译安装

  • mongo-c-driver-1.13.0
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local .. 
make
sudo make install
  • mongo-cxx-driver-r3.4.0
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo make
sudo make install

需要注明的是需要sudo make ,目的是自动安装依赖库EP_mnmlstc_core

windows编译安装

win 64位

  • mongo-c-driver-1.13.0
cmake.exe -G "Visual Studio 15 Win64"  "-DCMAKE_INSTALL_PREFIX=D:\mongo\mongocdriver" ..
  • mongo-cxx-driver-r3.4.0
cmake -G "Visual Studio 15 Win64" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=D:\mongo\mongocxx -DCMAKE_PREFIX_PATH=D:\mongo\mongocdriver -DLIBBSON_DIR=D:\mongo\mongocdriver -DLIBMONGOC_DIR=D:\mongo\mongocdriver -DBOOST_ROOT=D:\develop\boost_1_66_0 -DBOOST_LIBRARYDIR=D:\develop\boost_1_66_0\lib64-msvc-14.1  ..

win 32位

cmake.exe -G "Visual Studio 15"  "-DCMAKE_INSTALL_PREFIX=D:\mongo\mongocdriver" ..
  • mongo-cxx-driver-r3.4.0
cmake -G "Visual Studio 15" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=D:\mongo\mongocxx -DCMAKE_PREFIX_PATH=D:\mongo\mongocdriver -DLIBBSON_DIR=D:\mongo\mongocdriver -DLIBMONGOC_DIR=D:\mongo\mongocdriver -DBOOST_ROOT=D:\develop\boost_1_66_0 -DBOOST_LIBRARYDIR=D:\develop\boost_1_66_0\lib64-msvc-14.1  ..

工程引用

由于mongocxx使用了C++17的一些特性,如std::optionalstd::string_view,而windows下visual studio 2017默认没有开启C++17,因此需要手动开启C++17支持。

  • 开启方式

    visual studio 2017

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wiidL4ng-1655949804043)(visual开启C++17.png)]

或者在CMake中添加

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")

linux平台CMake添加

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std:c++17")

使用实例

  • mongocxx::client vs mongocxx::pool区别

参考:http://mongocxx.org/mongocxx-v3/connection-pools/

A standalone mongocxx::client uses a single-threaded algorithm to monitor the state of the cluster it’s connected to. When connected to a replica set, the thread “stops the world” every 60 seconds to check the status of the cluster. A mongocxx::pool, on the other hand, uses a separate background thread for each server in the cluster, each of which checks the status of the server it monitors every 10 seconds. Because of the performance advantages of monitoring the cluster in the background rather than “stopping the world”, it’s highly recommended to use a mongocxx::pool rather than a set of standalone clients if your application has access to multiple threads, even if your application only uses one thread.

  • 一个简单的实例
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

int main(int, char**) {
    auto mongouri = "mongodb://192.168.10.83:27017";
    mongocxx::instance inst{};
    mongocxx::client conn{ mongocxx::uri{mongouri} };
    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";

    collection.insert_one(document.view());
    auto cursor = collection.find({});

    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
    return 0;
}
  • 使用连接池
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/pool.hpp>
#include <mongocxx/instance.hpp>


int main(int, char**) {

    mongocxx::instance inst{};
    mongocxx::uri mongouri{ "mongodb://192.168.10.83:27017" };
    mongocxx::pool pool{mongouri};
    auto client = pool.acquire();
    auto collection = (*client)["testdb"]["testcollection"];
    bsoncxx::builder::stream::document document{};
    document << "hello" << "world";

    collection.insert_one(document.view());
    auto cursor = collection.find({});

    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }

    return 0;
}

其它实例

MongoDB CXX Driver examples

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-06-25 18:26:09  更:2022-06-25 18:28:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 19:18:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码