mongodb c++ 起步

【转自:http://blog.chinaunix.net/space.php?uid=7907749&do=blog&id=2037221

 

最近准备把空闲时间都发在mongodb的研究上,因此将有一系列的文章记录这个过程。
直接从官网下载了1.2.1的windows32版本:
解压之后写了个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\lib
2,项目-->属性(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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值