说明: * IN表示输入参数; * OUT表示输出参数;
(1)构造函数:
DBClientConnection(bool auto_connect, 0, double so_timeout);
(2)连接mongo:
bool connect(string server, string& errmsg);
示例:
bool auto_connect = true; double so_timeout = 3; string host = "127.0.0.1"; string port = "3003"; string errmsg = ""; DBClientConnection pConn = new DBClientConnection(auto_connect, 0, so_timeout); pConn->connect(host+":"+port, errmsg);
(3)查询
auto_ptr query(const string &ns, Query query, int nToReturn, int nToSkip,const BSONObj *fieldsToReturn, int queryOptions , int batchSize);
示例:
string db = "shool"; string collection = "student"; Query condition = QUERY("age"<<20); int limit = 10; int offset = 5; BSONObj columns = BSON("uid"<<1<<"name"<<1); auto_ptr cursor; cursor = pConn->query(db+"."+collection, condition, limit, offset, columns, 0, 0);
其效果相当于:
select uid,name from shool.student where age=20 limit 5,10;
对结果集的操作:
int uid=0; string name=""; while(cursor->more()) { BSONObj p = cursor->next(); uid = p["uid"].Int(); name = p["name"].String(); count << uid << " " << name << endl; }
(4)插入
void insert(const string &ns, BSONObj obj, int flags);
示例:
BSONObj insert = BSON("uid"<<10001<<"name"<<"skean1017");
pConn->insert(db+"."+collection, insert, 0);
其效果相当于:
insert into shool.student (uid, name) values (10001, “skean1017″);
(5)删除
void remove(const string &ns, Query query, bool justOne);
示例:
Query query = QUERY("name"<<"skean1017");
pConn->remove(db+"."+collection, query, true);
其效果相当于:
delete from shool.student where name=”skean1017″;
(6)修改
void update(const string &ns , Query query , BSONObj obj , bool upser , bool multi);
示例:
Query query = QUERY("uid"<<10001); BSONObj obj = BSON("$set"<<"name"<<"habadog1203");(#add可能有误,待改)
pConn->update(db+"."+collection, query, obj, false, false);
其效果相当于:
update shool.student set name=”habadog1203″ where uid=10001;