mongodb-c-driver使用詳解----windows環境

    網上的各種資源,無法正常編譯出windows環境下的lib、dll文件庫文件,只好自己動手,翻看官方文檔,沒想到官方的操作指引,也是不能達到預期,只好自己琢磨。爲方便以後使用,整理成文檔如下

一、介紹

     MongoDB C Driver也常被寫成“libmongoc”、“mongodb-c-driver”,是MongoDB官方C語言客戶端庫,用於和MongoDB交互。

       當處理DSON數據時(MongoDB的二進制數據) mongodb-c-driver依賴於libbson

二、編譯

依賴cmake工具編譯,所以,先安裝好cmake,配置好cmake環境變量,自行百度。

然後下載源碼:

源碼官方獲取渠道:http://mongoc.org/

github: https://github.com/mongodb/mongo-c-driver

此文檔所用版本爲:mongo-c-driver-1.16.2

 

下載後,解壓,打開命令行,cd進入根目錄以下爲命令 (我的解壓目錄爲:“D:\應用程序\MongoDb\mongo-c-driver\mongo-c-driver-1.16.2x86”)

官方指導文檔上的方法不可行,請按以下方法操作

cd /d D:\應用程序\MongoDb\mongo-c-driver\mongo-c-driver-1.16.2x86

 

cmake -G "Visual Studio 10 2010" "-DCMAKE_INSTALL_PREFIX=C:\mongo-c-driver" "-DCMAKE_PREFIX_PATH=C:\mongo-c-driver"

 

到根目錄下查看,發現生成了.sln文件,用VS打開,會發現一大堆工程,先編譯以下圏紅工程:

編譯成功後,再執行安裝工程,提取出lib、dll、頭文件等:

你會發現,在C:\mongo-c-driver目錄下生成了以下文件:

到此,編譯完成,下面說幾個注意點:

“10 2010”字樣,是針對不同版本的VS,可以自行選擇。x64版本的,“10 2010”應該寫成“10 2010 Win64”,默認是x86版本。備選項有:

 

       而“C:\mongo-c-driver”爲生成開發文件的路徑,可自行修改。

 

       針對不同的VS版本,按以上方法自行編譯不同的版本

 

三、使用

官方使用說明:http://mongoc.org/libmongoc/current/visual-studio-guide.html

創建個win32控制檯程序

msvc-create-project.pnguploading.4e448015.gif轉存失敗重新上傳取消http://mongoc.org/libmongoc/current/_images/msvc-create-project.png

 

選擇你對應和版本,注意別選錯了,不然你會抓狂:

 

在工程名中,右鍵菜單==》properties:

C:\mongo-c-driver\include\libbson-1.0

C:\mongo-c-driver\include\libmongoc-1.0

設置成正確的路徑即可,以上爲參考。

 

設置lib文件庫路徑:

C:\mongo-c-driver\lib\bson-1.0.lib

C:\mongo-c-driver\lib\mongoc-1.0.lib

 

設置動態庫的路徑:

PATH=c:/mongo-c-driver/bin

 

最後引入頭文件

#include <mongoc/mongoc.h>

 

寫一段測試代碼:

int _tmain(int argc, _TCHAR* argv[])

{

       mongoc_client_t *client;

       mongoc_collection_t *collection;

       mongoc_cursor_t *cursor;

       bson_error_t error;

       const bson_t *doc;

       const char *collection_name = "test";

       bson_t query;

       char *str;

       const char *uri_string = "mongodb://127.0.0.0/?appname=client-example";

       mongoc_uri_t *uri;

 

       mongoc_init ();

 

       uri = mongoc_uri_new_with_error (uri_string, &error);

       if (!uri) {

              fprintf (stderr,

                     "failed to parse URI: %s\n"

                     "error message:       %s\n",

                     uri_string,

                     error.message);

              return EXIT_FAILURE;

       }

 

       client = mongoc_client_new_from_uri (uri);

       if (!client) {

              return EXIT_FAILURE;

       }

 

       mongoc_client_set_error_api (client, 2);

 

       bson_init (&query);

 

#if 0

       bson_append_utf8 (&query, "hello", -1, "world", -1);

#endif

 

       collection = mongoc_client_get_collection (client, "test", collection_name);

       cursor = mongoc_collection_find_with_opts (

              collection,

              &query,

              NULL,  /* additional options */

              NULL); /* read prefs, NULL for default */

 

       while (mongoc_cursor_next (cursor, &doc)) {

              str = bson_as_canonical_extended_json (doc, NULL);

              fprintf (stdout, "%s\n", str);

              bson_free (str);

       }

 

       if (mongoc_cursor_error (cursor, &error)) {

              fprintf (stderr, "Cursor Failure: %s\n", error.message);

              return EXIT_FAILURE;

       }

 

       bson_destroy (&query);

       mongoc_cursor_destroy (cursor);

       mongoc_collection_destroy (collection);

       mongoc_uri_destroy (uri);

       mongoc_client_destroy (client);

       mongoc_cleanup ();

 

       return EXIT_SUCCESS;

}

 

官方教程:

http://mongoc.org/libmongoc/current/tutorial.html#installing

開發手冊:http://mongoc.org/libmongoc/current/guides.html

API手冊:http://mongoc.org/libmongoc/current/api.html

 

四、監控

可以監控MongoDB所有事件,待續…

官方教程:http://mongoc.org/libmongoc/current/application-performance-monitoring.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章