一、編譯mongodb c driver:
需要先安裝OpenSSL:(參見:http://bbs.aircheng.com/read-2222-1)
步驟:(MongoDB不需要授權登陸的話,就不需要OpenSSL,直接跳到第11步)
1.下載ActivePerl 5.24.0.2400
http://www.activestate.com/activeperl/downloads
2.安裝ActivePerl軟件
沒什么好說的,一步一步安裝即可。安裝成功后,設置環境變量。
例如我將ActivePerl安裝在C:\Perl64\目錄下,則選中Path,單擊編輯按鈕,
將C:\Perl64\site\bin;C:\Perl64\bin;加入其中,確認即可。
運行”CMD“命令,使用cd命令將運行目錄指向perl安裝目錄的eg文件夾,
執行“perl example.pl”若顯示“Hello from ActivePerl!”,則說明Perl安裝成功,
可以開始使用Perl的相關命令來進行OpenSSL的安裝了。
3.安裝Microsoft Visual Studio 2010
將C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64;
加入系統環境變量,具體以你安裝的路徑為准,
注意(64位)是 bin\amd64; 不是 bin; 多么痛的領悟。
4.安裝NASM
http://www.nasm.us/
當前穩定版本為:2.12.02
http://www.nasm.us/pub/nasm/releasebuilds/2.12.02/win64/

將nasm的路徑添加至環境變量
例如我的nasm安裝在 C:\Users\lenovo\AppData\Local\NASM 目錄
5.下載OpenSSL
https://www.openssl.org/source/
下載后,將其解壓縮至C:\openssl目錄下,這樣做便於后續操作
注意目錄結構,不要出現openssl-1.1.0c子目錄,
而是將openssl-1.1.0c目錄下的文件拷貝至C:\openssl目錄下
6.初始化編譯環境
因為Visual Studio 2010安裝在目錄中C:\Program Files (x86)\Microsoft Visual Studio 10.0\
這里使用的是在Visual Studio Tools文件夾下的Visual Studio x64 Win64命令提示(2010)工具。
然后右鍵:以管理員身份運行

進入Visual Studio 2010安裝目錄 C:\Program Files (x86)\Microsoft Visual Studio 10.0\中
執行命令:cd C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64
執行命令:vcvars64.bat
7.openssl的源文件已解壓至C:\openssl的目錄中,下面開始執行Configure
進入openssl 目錄下執行 perl Configure VC-WIN64A
執行結束后,如下圖所示

8.執行命令:nmake
如果執行成功,則等待輸入下一個命令
9.執行命令:nmake test
很順利的情況下:

10.執行命令:nmake install
當出現下面頁面的時候,證明你已成功:

11.編譯驅動:
http://mongoc.org/libmongoc/(api 官方文檔)
https://github.com/mongodb/mongo-c-driver(GitHub驅動源碼,這個 src/libbson 文件夾里面是空的)
驅動源碼下載:https://github.com/mongodb/mongo-c-driver/releases
build前需下載個最新的cmake安裝。


編譯完成之后在c:\mongo-c-driver目錄下有bin、include、lib三個文件夾,分別包含所需的dll、.h文件、lib。


在自己的項目中引入這些.h文件、lib、dll就可以使用 C API 了。
二、C API 的使用
1.連接MongoDB:
//只能調用一次mongoc_init
mongoc_init();
const char *uristr = "mongodb://user:pwd@host:port/";
//MongoDB不需要登錄認證的話,連接字符串中的用戶名:密碼@可以省略。
//connection to MongoDB.
m_client = mongoc_client_new (uristr);
if (!m_client)
{
return false;
}
m_pCollection = mongoc_client_get_collection(m_client, "myDatabase", "mycollection");
return true;
2.Insert操作:
bson_t query;
bson_init(&query);
BSON_APPEND_UTF8(&query,"name","chunxiao");
BSON_APPEND_INT32(&query,"age",NULL);
BSON_APPEND_DOUBLE(&query,"Price",66.0);
if (!mongoc_collection_insert(m_pCollection,MONGOC_INSERT_NONE,&query,NULL,&error))
{
AfxMessageBox(error.message);
}
bson_destroy(&query);
return true;
3.Find操作:
double age1 = 20;
double age2 = 22;
char name[10] = "chunxiao";
int sex = 0;
bson_init(&query);
bson_append_document_begin(&query,"age",3,&child);
bson_append_double(&child,"$gt",3,age1);
bson_append_document_end(&query,&child);
bson_append_document_begin(&query,"age",3,&child1);
bson_append_double(&child1,"$lte",4,age2);
bson_append_document_end(&query,&child1);
cursor = mongoc_collection_find(m_pCollection,MONGOC_QUERY_NONE,0,0,0,&query,NULL,NULL);
vector<string> vData;
while (mongoc_cursor_next(cursor,&docFind))
{
//方法一
bson_iter_t iter;
bson_iter_init(&iter,docFind);
if (bson_iter_find(&iter,"name"))
{
strcpy(str,bson_iter_utf8(&iter,NULL));
strtest = bson_iter_utf8(&iter,NULL);
if (strcmp(strtest.c_str(),"xiaochun")==0)
{
AfxMessageBox(str);
continue;
}
strtest = strtest.substr(strtest.find_first_of("x"),2);
AfxMessageBox(str);
}
bson_iter_init(&iter,docFind);//順序不一致的話,要重新初始化一下 iter,否則find不到
if (bson_iter_find(&iter,"age"))
{
int f = bson_iter_int32(&iter);
sprintf(str,"%d",f);
AfxMessageBox(str);
}
//方法二
char *str;
str = bson_as_json(docFind, NULL);
vData.push_back(str);
}
if (mongoc_cursor_error(cursor, &error))//mongoc_cursor_error 要在 mongoc_cursor_next 之后調用,否則獲取不到 error.message
{
cout << "An error occurred:" << error.message << endl;
mongoc_cursor_destroy(cursor);
bson_destroy(&query);
return false;
}
mongoc_cursor_destroy(cursor);
bson_destroy(&query);
return true;
4.Delete操作:
double age = 21;
bson_init(&query);
bson_append_document_begin(&query,"age",3,&child);
bson_append_double(&child,"$gt",3,age);
bson_append_document_end(&query,&child);
BSON_APPEND_UTF8(&query,"name","chun.xiao");
if (!mongoc_collection_remove(m_pCollection,MONGOC_REMOVE_NONE,&query,NULL,&error))
{
AfxMessageBox(error.message);
}
bson_destroy(&query);
return true;
5.UpDate操作:
bson_init(&query);
BSON_APPEND_UTF8(&query,"name","chunxiao");
bson_t *docUpDate = bson_new();
bson_append_document_begin(docUpDate,"$set",-1,&child);
BSON_APPEND_UTF8(&child,"name","xiaochun");
bson_append_document_end(docUpDate,&child);
bson_init(&child);
bson_append_document_begin(docUpDate,"$set",-1,&child);
BSON_APPEND_UTF8(&child,"age","21");
bson_append_document_end(docUpDate,&child);
if (!mongoc_collection_update(m_pCollection,MONGOC_UPDATE_NONE,&query,docUpDate,NULL,&error))
{
AfxMessageBox(error.message);
}
bson_destroy(&query);
bson_destroy(docUpDate);
return true;
百度雲(13207134391):
編譯 C driver:
MongoDB\C API\安裝MongoDB以及編譯 C driver
Demo:
MongoDB\C API\C API 操作MongoDB
