C++ Language Center
点击打开链接
C++ driver download
编译驱动之前需要安装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
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; }
点击打开链接
C++ driver download
编译驱动之前需要安装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
。。。。。。。。。。。。。。。。。。。。华丽分界线。。。。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。华丽的分界线。。。。。。。。。。。。。。
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; }