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):是否只删除匹配的第一条





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值