一、mongoDB c、c++API介绍
mongoDB有两个接口库:mongo-c-driver和mongo-cxx-driver
1.1、mongo-c-driver:c代码接口库
下载链接:https://github.com/mongodb/mongo-c-driver
1.2、mongo-cxx-driver:c++代码接口库,是在mongo-c-driver的基础上进行二次封装
下载链接:https://github.com/mongodb/mongo-cxx-driver
最新版本:MongoDB C++11 Driver 3.4.0
注意:此库需要boost支持
二、准备工作
1、安装VS 2017:
2、安装cmake:
下载地址:https://github.com/Kitware/CMake/releases/download/v3.13.3/cmake-3.13.3-win64-x64.zip
3、安装boost:
三、编译mongo-c-driver
1、把mongo-c-driver-1.13.0.tar.gz解压到目录:C:\mongocode\mongo-c-driver-1.13.0
2、cmake
1)打开cmd
2)cd到目录:C:\mongocode\mongo-c-driver-1.13.0\build
3)执行cmake命令:
cmake.exe -G "Visual Studio 15 2017 Win64" "-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver" "-DCMAKE_PREFIX_PATH=C:\mongo-c-driver" ..
3、创建编译输出目录:C:\mongo-c-driver
注意:此目录必须手动创建,否则编译会报错
4、编译:
1)用VS2017打开:C:\mongocode\mongo-c-driver-1.13.0\build\mongo-c-driver.sln
2)选择Debug、x64
3)选中工程:ALL_BUILD
4)点击编译菜单开始编译ALL_BUILD
5)选中工程:INSTALL
6)点击编译菜单开始编译INSTALL
7)编译完成,生成在C:\mongo-c-driver目录下
注意:bson不需要单独编译
四、编译mongo-cxx-driver
1、把mongo-cxx-driver-r3.4.0.zip解压到目录:C:\mongocode\mongo-cxx-driver-r3.4.0
2、cmake
1)打开cmd
2)cd到目录:C:\mongocode\mongo-c-driver-1.13.0\build
3)执行cmake命令:
cmake.exe -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=C:\mongo-cxx-driver -DCMAKE_PREFIX_PATH=C:\mongo-c-driver -DBOOST_ROOT=C:\boost\include -DCMAKE_CXX_STANDARD=11 -DCMAKE_CXX_FLAGS="/Zc:__cplusplus" ..
注意:-DBOOST_ROOT=C:\boost\include 是boost目录
3、创建编译输出目录:mongo-cxx-driver
注意:此目录必须手动创建,否则编译会报错
4、编译:
1)用VS2017打开:C:\mongocode\mongo-cxx-driver-r3.4.0\build\build\MONGO_CXX_DRIVER.sln
2)选择Debug、x64
3)选中工程:ALL_BUILD
4)点击编译菜单开始编译ALL_BUILD
编译失败。。。。
5)问题一:
解决方法:修改工程设置,所有工程都按下图修改
6)问题二:
解决方法:添加宏(_ENABLE_EXTENDED_ALIGNED_STORAGE)
7)编译成功:
8)选中工程:INSTALL
9)点击编译菜单开始编译INSTALL
10)编译完成,生成在C:\mongo-cxx-driver目录下
注意事项:目录结构不要随意更改,否则容易出错
五、VS2017调用实例
1、新建工程:mongotest
2、复制include文件到工程目录下:
3、复制lib到工程目录下:
4、复制dll到工程目录下:
5、设置工程include目录:
注意:需要包含boost库目录
6、设置附加依赖性:
7、设置附加库目录:
8、demo代码:
#include "pch.h"
#include <iostream>
#include "bsoncxx/builder/stream/document.hpp"
#include "mongocxx/instance.hpp"
#include "mongocxx/uri.hpp"
#include "mongocxx/client.hpp"
int main()
{
mongocxx::instance instance{};
mongocxx::uri uri("mongodb://127.0.0.1:27017");
mongocxx::client client(uri);
// 数据库
mongocxx::database db = client["mytestdb"];
// 集合
mongocxx::collection coll = db["testcoll"];
// 插入
try
{
for (int i=1; i<=10; ++i)
{
auto builder = bsoncxx::builder::stream::document{};
builder << "userid" << i
<< "name" << "username";
coll.insert_one(builder.view());
std::cout << "insert : " << i << "\n";
}
}
catch (const std::exception& e)
{
std::cout << "insert error : " << e.what();
}
// 查找
try
{
auto cursor = coll.find(bsoncxx::builder::stream::document{} << "name" << "username" << bsoncxx::builder::stream::finalize);
if (cursor.begin() != cursor.end())
{
for (auto & doc : cursor)
{
std::string Name = doc["name"].get_utf8().value.to_string();
std::cout << "find : userid[" << doc["userid"].get_int32() << "] name[" << Name << "]\n";
}
}
}
catch (const std::exception& e)
{
std::cout << "find error : " << e.what();
}
// 更新
try
{
coll.update_one(bsoncxx::builder::stream::document{} <<
"userid" << 3 << bsoncxx::builder::stream::finalize,
bsoncxx::builder::stream::document{} << "$set" << bsoncxx::builder::stream::open_document
<< "name" << "username3333333"
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::finalize);
std::cout << "update : userid[3] name[username3333333]" << "\n";
}
catch (const std::exception& e)
{
std::cout << "update error : " << e.what();
}
// 删除数据
try
{
auto filter_builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value filter_value = filter_builder << "userid" << 7 << bsoncxx::builder::stream::finalize;
coll.delete_one(filter_value.view());
std::cout << "delete : userid[7]" << "\n";
}
catch (const std::exception& e)
{
std::cout << "delete error : " << e.what();
}
// 再次查找
try
{
auto cursor = coll.find(bsoncxx::builder::stream::document{} << bsoncxx::builder::stream::finalize);
if (cursor.begin() != cursor.end())
{
for (auto & doc : cursor)
{
std::string Name = doc["name"].get_utf8().value.to_string();
std::cout << "find again : userid[" << doc["userid"].get_int32() << "] name[" << Name << "]\n";
}
}
}
catch (const std::exception& e)
{
std::cout << "find error : " << e.what();
}
getchar();
}
9、运行结果: