C++操作MongoDB
目录
- 目录
- 编译信息
- 说明
- 安装mongocxx driver驱动
- 增删改查
- 参考文档
编译信息
作者:xzf
创建时间:2021/10/12
更新时间:2021/10/13
说明
本文主要说明,在linux下实现C++操作MongoDB,实现增删减查,前提是已经安装好 cmake,和 MongDB,然后需要编译 mong-cxx-driver ,如果是编译最新版本,Ubuntu自带的 mongo-c-driver版本可能不够,需要自己编译mongo-c-driver,根据官方文档有提供不同版本需求 mongo-c-driver的版本。
环境:
- 系统:
Linux zhenfeixu-linux 5.11.0-37-generic #41~20.04.2-Ubuntu SMP Fri Sep 24 09:06:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
- MongoDB:
MongoDB shell version v5.0.3
安装mongocxx driver驱动
参照官方编译文档:http://mongocxx.org/mongocxx-v3/installation/linux/
非官方文档:C++(Linux)操作MongoDB
1. 安装mongo-c-driver
根据官方文档可知,由于采用的是mongo-cxx-3.6.6 版本,至少需要是多少libmongoc 1.17.0 (原文:For mongocxx-3.6.x, libmongoc 1.17.0 or later is required.),所以需要先安装mongo-c-driver。
安装mongo-c-driver文档:http://mongoc.org/libmongoc/current/installing.html
本文采用的是下载发布版本里1.19.1版本。
# 构建
$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.19.1/mongo-c-driver-1.19.1.tar.gz
$ tar xzf mongo-c-driver-1.19.1.tar.gz
$ cd mongo-c-driver-1.19.1
$ mkdir cmake-build
$ cd cmake-build
$ cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..
# 安装
$ cmake --build .
$ sudo cmake --build . --target install
注意:如果不安装第一步,可能会报错,版本不够。
2. 安装mongo-cxx-driver
# 获取源码
$ git clone https://github.com/mongodb/mongo-cxx-driver.git --branch releases/stable --depth 1
# 进入build
$ cd mongo-cxx-driver/build
# 指定版本和安装位置
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
# 安装
$ make && sudo make install
3. 进行验证代码运行的时候,会报错
# 运行程序
$ ./create
./create: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory
# 检测缺少的动态库
$ ldd create
linux-vdso.so.1 (0x00007ffebd9fc000)
libmongocxx.so._noabi => not found #错误连接
libbsoncxx.so._noabi => not found #错误连接
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f997a61c000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f997a601000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f997a40f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f997a2be000)
/lib64/ld-linux-x86-64.so.2 (0x00007f997a81c000)
# 查找文件在哪
$ sudo find / -name libmongocxx.so._noabi
$ sudo find / -name libbsoncxx.so._noabi
# 发现是个链接文件,指向了真实文件
$ ls -l /usr/local/lib/libmongocxx.so._noabi
lrwxrwxrwx 1 root root 20 10月 12 15:40 /usr/local/lib/libmongocxx.so._noabi -> libmongocxx.so.3.6.6
$ ls -l /usr/local/lib/libbsoncxx.so._noabi
lrwxrwxrwx 1 root root 19 10月 12 15:40 /usr/local/lib/libbsoncxx.so._noabi -> libbsoncxx.so.3.6.6
# 进入/usr/local/lib
$ cd /usr/local/lib
# 删除原来的连接文件
$ sudo rm libbsoncxx.so._noabi
$ sudo rm libmongocxx.so._noabi
# 建立新的连接文件
$ sudo ln -s /usr/local/lib/libmongocxx.so.3.6.6 /usr/local/lib/libmongocxx.so._noabi
$ sudo ln -s /usr/local/lib/libbsoncxx.so.3.6.6 /usr/local/lib/libbsoncxx.so._noabi
# 重新加载库
$ sudo ldconfig
# 验证,这时候应该是正常的,然后就能正常编译了
$ ldd create
参考:安装mongo-c-driver和mongo-cxx-driver中遇到的部分问题及解决办法
增删减查
参考官方例程:https://github.com/mongodb/mongo-cxx-driver/blob/master/examples
目前3.6.6文档不建议使用bsoncxx::stream
,故使用bsoncxx::basic
而且3.6.6版本已经没有stream的文件。
Use of the stream builder is discouraged. See http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working-with-bson/#stream-builder for more details.
#include <chrono>
#include <iostream>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/types.hpp>
#include <bsoncxx/json.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(int,char**){
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
auto db = conn["test"];
// 增加insert
// bsoncxx::document::value restaurant_doc =make_document(kvp("add","add"));
// auto res= db["new"].insert_one(std::move(restaurant_doc));
// 减少remove
// db["test"].delete_one(make_document(kvp("name","taka")));
// 修改update
// db["test"].replace_one(make_document(kvp("add","add")),make_document(kvp("add","update")));
// 查check
auto cursor = db["test"].find(make_document(kvp("add","update")));
// 如果没有查询到,就直接返回
if(cursor.begin()!=cursor.end())
{
for(auto&& doc : cursor)
{
std::cout<< bsoncxx::to_json(doc)<<std::endl;
}
}
return 0;
}