MongoDB Primary(編譯MongoDB,C++串連MongoDB測試, GLIB INSTALL)

來源:互聯網
上載者:User

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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.