MongoDB的c++用法

連接Mongo數據庫

try{
	mongo::DBClientConnection c;
	c.connect("localhost");
}
catch(const mongo::DBException &e)
{
	std::cout<<e.what()<<endl;
}
bool connect(string server,&string errmsg)

返回值:成功/失敗

server:連接的服務器,本地“127.0.0.1“+”.“+port端口號

errmsg:出錯信息


插入值

往數據庫中插入數據必須創建BSONObj類的對象,BSONObj下各組件都可以叫做BSONElement對象。使用BSONObjBuilder構造各種BSON對象,

用BSONObjIterator來遍歷各BSON對象。

BSONObjBuilder b;
b<<"name"<<"joe"<<"age"<<33;
BSONObj p=b.obj();

等同於

BSONObj p=BSON("name"<<"joe"<<"age"<<33) 若插入的值爲vector基本類型容器,則可以直接插入到行中,不需要循環插入,但是讀取的時候需要循環讀取

BSONObj insert=BSON("uid"<<10001<<"name"<<"guojing");
c.insert("mydb.student",insert)


BSONObjBuilder build;
std::vector<BSONObj> all_mythical_vec;
	build << DBMythical::ID << player->role_id()
		<< DBMythical::TYPE << all_mythical_vec;
作爲一個大類型插入數據庫可以用vector插入整個BSON數組

void insert(const string &ns,BSONObj obj,int flags)

ns:db_name.collection_name

obj:插入的列

flag:默認爲0



讀取值

輸出對象的數量:
	cout<<c.count("mydb.student")
auto_ptr<DBClientCursor> cursor =c.query("mydb.student",BSONObj());//一個空的BSON對象,query條件爲空表示搜索全表

Query condition=QUERY("age"<<20);
BSONOBj columns=BSON("uid"<<1<<"name"<<1);
auto_ptr cursor=c.query("mydb.student",condition,limit,offset,columns,0,0);
while(cursor->more)
{
	BSONObj p=cursor->next();
	uid=p["uid"].Int();
	name=p["name"].string();
}

auto_ptr query(const string &ns, Query query, int nToReturn, int nToSkip,const BSONObj *fieldsToReturn, int queryOptions , int batchSize);

  • 返回值:結果集
  • ns(IN):命名空間,db_name.collection_name
  • query(IN):查詢的條件,相當於mysql中的where
  • nToReturn:返回結果條數,相當於mysql中的limit
  • nToSkip:跳過的結果條數,相當於mysql中的offset
  • fieldsToReturn:返回列集合
  • queryOptions:詳見QueryOptions這個枚舉,填0即可
  • batchSize:未說明


讀取值:

        複雜邏輯條件:db.things.find( {$and: [ { $or : [ {'a':1} , {'b':2} ] }, "c":1 ]  }) 

	std::vector<BSONObj> bson_vec;
	BSONObj obj=BSON( 'a' << 1);
	BSONObj obj2=BSON( 'b' << 2);
	bson_vec.push_back(obj);
	bson_vec.push_back(obj2);
	std::vector<BSONObj> bson_vec_and;
	bson_vec_and.push_back(BSON( 'c' <<  1));
	bson_vec_and.push_back(BSON("$or"<<bson_vec));
	data_map->push_multithread_query(COLLECTION_CONTACT,BSON("$and"<<bson_vec_and));
用getObjectField得到一個字段的整體數組內容,用embeddedObject得到特定列

BSONObj res = this->conection().findOne(DBMythical::COLLECTION,
				QUERY(DBMythical::ID << player->role_id()));

BSONObjIterator ite_all_mythical(res.getObjectField(DBMythical::TYPE.c_str()));

BSONObj bson_all_mythical=ite_all_mythical.next().embeddedObject();
		Int64 monster_id=bson_all_mythical[DBMythical::MONSTER_ID].numberInt();


排序:

進一步若要使返回的結果集按name的字母序排序,則可通過使用Query::sort()來給query表達式增加一個更改項

auto_ptr<DBClientCursor> cursor = c.query("mydb.student", QUERY("uid" << 1001 ).sort("name"));


索引:

若用uid作爲索引,用來加速查詢

c.ensureIndex("mydb.student",BSON("uid"<<1));

ensureIndex會先做同樣的索引存在檢測,若無則創建。ensureIndex是智能的,不會向服務端重複發送,因而就算多次調用它也是安全的。



修改:

c.update("mydb.student",QUERY("uid"<<1001),BSON("$inc"<<BSON("name"<<"liming")),false,true);

void update(const string &ns , Query query , BSONObj obj , bool upser , bool multi);
  • ns(IN):命名空間,db_name.collection_name
  • query(IN):查詢條件
  • obj(IN):修改後的值
  • upser(IN):是否upser,如果不存在則插入記錄
  • multi(IN):是否爲符合文


刪除:

Query query=QUERY("name"<<"guojing");
c.remove("mydb.student",query,true);
void remove(const string &ns, Query query, bool justOne);
  • ns(IN):命名空間,db_name.collection_name
  • query(IN):查詢條件
  • justOne(IN):是否只刪除匹配的第一條





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章