MongoDB
windows下编译Mongodby的mongodb cxx driver驱动
首选这个真的不知道为什么没有现成的驱动库,可能是我没有找到。就是官方下载了源码编译
一. 在编译成mongodb cxx drive驱动前得先编译mongodb c drive驱动,注意这两个不一样。一个是c驱动一个是cxx驱动。试过在cxx里调用c的库用不了。
首先下载和编译mongodb c drive.
选准备如下资料和工具:
mongodb c drive: https://github.com/mongodb/mongo-c-driver/releases
mongodb cxx drive: https://github.com/mongodb/mongo-cxx-driver
Cmake编译器: https://cmake.org/
Boost库: http://www.boost.org/
以上是自己准备的这样自己编译成功过,python2.7我已安装不知道是否有关系。网上有些资料还需要准备如下几项:
python2.7: https://www.python.org/downloads/windows/
scons是一个python写的自动构造工具,同Linux的make工具功能相似。与之关联的SConstruct文件也即类似make工具的makefile文件,描述了编译和链接的内容和方式。在这里就是用scons这个工具来编译生成mongoclient.lib的这个是网上使用了scons而非cmake编译构造
scons: http://www.scons.org/
网上的一些编译方法除了使用cmake更换成scons其他都心需要。
MongoDB C Driver英文版详细说明
二、以下使用自己编译成功的案例,使用的是cmake编译。
- 下载mongo-c-drive的C版本驱动,解压后进入mongo_c_driver_1.7\src\libbson目录下
cd mongo-c-1.7.0\src\libbson
cmake -G "Visual Studio 14 2015 Win64" "-DCMAKE_INSTALL_PREFIX=E:\CXX\mongo-c-driver" "-DCMAKE_BUILD_TYPE=Release"
- "-DCMAKE_INSTALL_PREFIX=E:\CXX\mongo-c-driver"是最终输出的目录。
IN - "-DCMAKE_BUILD_TYPE=Release"这是编译类型,默认是debug编译模式。
msbuild.exe /p:Configuration=Release ALL_BUILD.vcxproj
msbuild.exe /p:Configuration=Release INSTALL.vcxproj
- 运行上面的命令后编译了libbson库,再到mongo_c_driver_1.7.0根目录下,运行:
cd mongo-c-driver-1.7.0
cmake -G "Visual Studio 14 2015 Win64" "-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver" "-DCMAKE_PREFIX_PATH=C:\mongo-c-driver" "-DCMAKE_BUILD_TYPE=Release"
- "-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver"输出的目录。
- "-DCMAKE_PREFIX_PATH=C:\mongo-c-driver"搜索路径,将第一步编译的输出目录增加到此处。
- "-DCMAKE_BUILD_TYPE=Release"是编译模式,默认debug.
- "-DBSON_ROOT_DIR=C:\Downloads\usr"是刚才对应的libbson编译生成的指它的目录同样生成了对应的
msbuild.exe /p:Configuration=Release ALL_BUILD.vcxproj
msbuild.exe /p:Configuration=Release INSTALL.vcxproj
- 最终生成编译出的文件。这样mongo_c_driver就编译成功了。
三、编译mongo-cxx-driver
- 下载:https://github.com/mongodb/mongo-cxx-driver 解压后在mongo-cxx-driver目录下运行:
cd mongo-cxx-driver-r3.1.3
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=C:/mongo-cxx-driver -DCMAKE_PREFIX_PATH=c:\mongo-c-driver -DBOOST_ROOT=C:\boost_1_63_0
#多余的参数
-DLIBBSON_DIR=C:\Downloads\usr -DLIBMONGOC_DIR=C:\Downloads\usr
- -DCMAKE_INSTALL_PREFIX指定编译输出的目录。
- -DLIBBSON_DIR和-DLIBMONGOC_DIR分别指定刚才编译输出libbson和mongo-c-driver的目录。
- -DBOOST_ROOT是指定boost所在的根目录。
- 以上编译后使用以下命令生成:
msbuild.exe ALL_BUILD.vcxproj
msbuild.exe INSTALL.vcxproj
3.以上两部已经能生成bin,include,lib文档了,网上还有一命令提供如下:这个应该是指定一些boost的dll和lib库的。如果没有安装scons可以忽略这步,在引入库时自行指定。
scons --64 --cpppath="D:\rep\devel\boost_1_62_0_bin" --libpath="D:\rep\devel\boost_1_62_0_bin\libs" install --dbg=on
四、最后提供英文版的一个官方编译文档
https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/
MongoCxx的应用
头文件中包含的头文件和空间名命
//*.h
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
打开和连接数据库
mongocxx::instance instance{}
mongocxx::uri uri=("mongodb://localhost:20717"); //连接的数据库地址
mongocxx::client client(uri);
mongocxx::db =client["mydb"]; //要打开的数据库
Find a Single Document in a Collection
bsoncxx::stdx::optional<bsoncxx::document::value> maybe_result=collection.find_one(document{}<<finalize);
if(maybe_result){
//do something with *maybe_result;
}
查找所有文档表
mongocxx::cursor cursor=collection.find(document{}<<finalize);
for(auto doc:cursor){
std::cout<<bsoncxx::to_json(doc)<<std::endl;
}
查找示例一:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::finalize;
//两个转换函数当出现乱码时右适当转换。
//UTF-8 to string
std::string UTF8_To_string(const std::string & str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr = pBuf;
delete[]pBuf;
delete[]pwBuf;
pBuf = NULL;
pwBuf = NULL;
return retStr;
}
//string to UTF-8
std::string string_To_UTF8(const std::string & str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete[]pwBuf;
delete[]pBuf;
pwBuf = NULL;
pBuf = NULL;
return retStr;
}
int main(int, char **) {
mongocxx::instance inst{};
mongocxx::client conn{ mongocxx::uri{ "mongodb://10.99.40.10:27018" } };
auto coll = conn["hqft_film"]["ceshi1"];
//coll.drop();
/*
// Insert a test document
auto joe = document{} << "user info" << open_document << "user name"
<< "Joe" << close_document << finalize;
auto result = coll.insert_one(joe.view());
std::cout << "Inserted " << result->inserted_id().get_oid().value.to_string()
<< std::endl;
*/
// Create the query filter
auto filter = document{} << u8"检查人"
<< u8"乔峰" << finalize;
// Create the find options with the projection
mongocxx::options::find opts{};
opts.projection(document{} << "maya_check" << 1 << finalize);
// Execute find with options
auto cursor = coll.find(filter.view(), opts);
for (auto &&doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
system("pause");
return 1;
}