一、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、運行結果: