【轉自:http://blog.chinaunix.net/space.php?uid=7907749&do=blog&id=2037221】
最近準備把空閑時間都發在mongodb的研究上,因此將有一系列的文章記錄這個過程。直接從官網下載了1.2.1的windows32版本:http://downloads.mongodb.org/win32/mongodb-win32-i386-1.2.1.zip解壓之後寫了個runserver.bat檔案:E:\lenkydatasource\mongodb\1.2.1\mongodb-win32-i386-1.2.1\bin\mongod.exe --dbpath E:\lenkydatasource\mongodb\1.2.1\lenkytest\db --port 55555執行該檔案即可啟動mongodb服務程式。解壓檔案內還包含了c++的標頭檔和庫檔案
E:\lenkydatasource\mongodb\1.2.1\mongodb-win32-i386-1.2.1\include
E:\lenkydatasource\mongodb\1.2.1\mongodb-win32-i386-1.2.1\lib利用它們來寫mongodb客戶程式:
首先需要有boost,而mongodb-win32-i386-1.2.1.zip裡提供的mongodb庫檔案
E:\lenkydatasource\mongodb\1.2.1\mongodb-win32-i386-1.2.1\lib\mongoclient.lib
是用vs2008 + boost_1_35_0編譯出來的,懶得重新編譯mongodb,因此也對應的使用vs2008 + boost_1_35_0編譯環境即可使用這個mongoclient.lib,boost_1_35_0從boost官網下載即可。編譯boost分兩步:
一:執行E:\lenkydatasource\mongodb\1.2.1\boost_1_35_0\boost_1_35_0\tools\jam\build_dist.bat二:將第一步產生的bjam.exe拷貝到E:\lenkydatasource\mongodb\1.2.1\boost_1_35_0\boost_1_35_0,然後執行:
cd E:\lenkydatasource\mongodb\1.2.1\boost_1_35_0\boost_1_35_0
bjam.exe link=static threading=multi variant=release runtime-link=static --without-python --toolset=msvc-9.0完成後即會產生很多lib庫,比如libboost_program_options-vc90-mt-gd-1_35.lib,把這些lib庫檔案(搜尋lib關鍵字)全部拷貝到E:\lenkydatasource\mongodb\1.2.1\boost_1_35_0\boost_1_35_0\uselib目錄(uselib目錄是我自己建立的)接下來利用vs2008建立工程test,包含檔案first.cpp,並做下設定:
1,工具-->選項-->項目和解決方案-->VC++ 目錄
加上對應的包含檔案:
E:\lenkydatasource\mongodb\1.2.1\boost_1_35_0\boost_1_35_0
E:\lenkydatasource\mongodb\1.2.1\mongodb-win32-i386-1.2.1\include\mongo
加上對應的庫檔案:
E:\lenkydatasource\mongodb\1.2.1\boost_1_35_0\boost_1_35_0\uselib
E:\lenkydatasource\mongodb\1.2.1\mongodb-win32-i386-1.2.1\lib2,項目-->屬性(Alt+F7)-->配置屬性-->C/C++-->代碼產生-->執行階段程式庫
選為多線程(/MT),即是靜態,不要用DLL。編譯執行,結果:
time:10.797s
insert finished
time:0.453s
query finished
time:0.094s
remove finished這個過程我遇到很多問題,一一解決後,正確流程就是上面這樣,也不知道遺漏了什麼沒敘述沒有。first.cpp檔案內容:
// first.cpp#include <iostream>#include "client/dbclient.h"#pragma comment(lib, "mongoclient.lib")
#pragma comment(lib, "wsock32.lib")using namespace std;int main( int argc, const char **argv ) {
int i;
clock_t start, finish;
string errmsg;
string table = "test.test";
int record = 100000;
mongo::DBClientConnection conn;// connect db server
if (!conn.connect(string("127.0.0.1:55555"), errmsg)) {
cout << "couldn't connect to server:" << errmsg << endl;
return -1;
}
// insert test data
start = clock();
for (i = 0; i < record; i ++) {
mongo::BSONObjBuilder query;
query << "user" << i << "pwd" << i << "age" << i;
query << "email" << i << "address" << i << "phone" << i;
conn.insert(table, query.obj());
}
finish = clock();
cout << "time:" << (double)(finish - start) / CLOCKS_PER_SEC << "s" << endl;
cout << "insert finished" << endl;// query test data
start = clock();
{
mongo::BSONObjBuilder query;
//query.append("user" , 1);
auto_ptr<mongo::DBClientCursor> cursor = conn.query(table, query.obj());
while (cursor->more()) {
mongo::BSONObj obj = cursor->next();
//cout << obj.jsonString() << endl;
}
}
finish = clock();
cout << "time:" << (double)(finish - start) / CLOCKS_PER_SEC << "s" << endl;
cout << "query finished" << endl;// remove test data
start = clock();
{
mongo::BSONObjBuilder query;
//query.append("user" , 1);
conn.remove(table, query.obj());
}
finish = clock();
cout << "time:" << (double)(finish - start) / CLOCKS_PER_SEC << "s" << endl;
cout << "remove finished" << endl;// waiting
cin >> errmsg;
return 0;
}