C++ Language Center
點擊開啟連結
C++ driver download
點擊開啟連結
Scons安裝步驟:cd build/sconspython setup.py install
編譯驅動之前需要安裝pcre 和 scons[root@:~/mongo-cxx-driver-v1.8]#scons經過一段時間的組建,產生libmongoclient.so:[root@:~/mongo-cxx-driver-v1.8]#lsauthTest clientTest firstExample libmongoclient.a LICENSE.txt SConstruct whereExampleclient 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.gzmake -f Makefile.refJS_DIST=/usr make -f Makefile.ref export編譯mongoDB並installtar -xvf mongodb-src-r1.4.4.tar.gzcd mongodb-src-r1.4.4scons --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 -L/usr/local/lib/ -I/usr/local/include/ -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;}