0
回答
MongoDB Primary(编译MongoDB,C++连接MongoDB测试, GLIB INSTALL)
C++ Language Center
点击打开链接


C++ driver download

点击打开链接


Scons安装步骤:
cd build/scons
python setup.py install

编译驱动之前需要安装pcre 和 scons
[root@:~/mongo-cxx-driver-v1.8]#scons
经过一段时间的组建,生成libmongoclient.so:
[root@:~/mongo-cxx-driver-v1.8]#ls
authTest  clientTest  firstExample    libmongoclient.a   LICENSE.txt  SConstruct     whereExample
client    config.log  httpClientTest  libmongoclient.so  mongo        secondExample

拷贝至 /usr/local/lib下
[root@:~/mongo-cxx-driver-v1.8]#cp libmongoclient.so /usr/local/lib

安装 boost lib 
./bootstrap.sh
./bjam install --prefix=/usr

GLIB INSTALL

glib install
./configure --prefix=/usr 
make;make install 
gcc 'pkg-config --cflags --libs glib-2.0'
gcc -_/usr/lib/glib-2.0/include/ -I/usr/include/glib-2.0 -lglib-2.0 XX.c

。。。。。。。。。。。。。。。。。。。。华丽分界线。。。。。。。。。。。。。。。。。。。。。。。。。


另外如果你编译MongoDB的源码需要下载依赖包 
ftp://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz
make -f Makefile.ref
JS_DIST=/usr make -f Makefile.ref export


编译mongoDB并install
tar -xvf mongodb-src-r1.4.4.tar.gz
cd mongodb-src-r1.4.4
scons --full install


另外如果你没有boost库 ,还需要安装boost | ./bootstrap.sh -> ./bjam install --prefix=/usr/local
所有安装完后,/usr/loca  include 和 libl下会有相应的mongodb的文件

。。。。。。。。。。。。。。。。。。。华丽的分界线。。。。。。。。。。。。。。

1 .C++简单连接MongoDB
#include <iostream>
#include "mongo/client/dbclient.h"
using namespace std;
using namespace mongo;

void run() {
   DBClientConnection c;
   c.connect("localhost"); //add port,c.connect("localhost:27017")
}

int main(void)
{
   try {
       run();
       cout<<"connected ok"<<endl;
   }catch(DBException& e){ 
       cout<<"caught"<<e.what()<<endl;
   }
   return 0;

}

编译:
[root@:~/svn/mongoDB]#g++ main.cpp -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options
运行:
[root@:~/svn/mongoDB]#./a.out 
connected ok

2.MongoDB自带的测试
#include <iostream>
#include "mongo/client/dbclient.h"
using namespace std;
using namespace mongo;

void run() {
   DBClientConnection c;
   c.connect("localhost"); //add port,c.connect("localhost:27017")
}

int main(void)
{
   try {
       run();
       cout<<"connected ok"<<endl;
   }catch(DBException& e){ 
       cout<<"caught"<<e.what()<<endl;
   }
   return 0;
}

#include <iostream>
#include "mongo/client/dbclient.h"

using namespace mongo;

void printIfAge(DBClientConnection& c, int age) {
	auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );
	while( cursor->more() ) {
		BSONObj p = cursor->next();
		cout << p.getStringField("name") << endl;
	}
}

void run() {
	DBClientConnection c;
	c.connect("localhost"); 
	cout << "connected ok" << endl;
	BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
	c.insert("tutorial.persons", p); /**< 向person表中插入数据 */
	p = BSON( "name" << "Jane" << "age" << 40 );
	c.insert("tutorial.persons", p);
	p = BSON( "name" << "Abe" << "age" << 33 );
	c.insert("tutorial.persons", p);
	p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
	c.insert("tutorial.persons", p);

	c.ensureIndex("tutorial.persons", fromjson("{age:1}"));

	cout << "count:" << c.count("tutorial.persons") << endl; /**< 显示person表中的数据数目 */

	auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());
	while( cursor->more() ) {
		cout << cursor->next().toString() << endl;
	}

	cout << "\nprintifage:\n";
	printIfAge(c, 33);
}

int main() {
	try {
		run();
	}
	catch( DBException &e ) {
		cout << "caught " << e.what() << endl;
	}
	return 0;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

  

原文链接:http://blog.csdn.net/crazyjixiang/article/details/6599840
举报
晨曦之光
发帖于7年前 0回/905阅
顶部