qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

PHP操作MongoDB 数据库总结记录

最近有个项目,需要用php操作mongoDb数据,所以了解下mongoDb为此整理了下,常见的操作......
  1,连接MongoDB数据库
$conn = new Mongo();
其他链接方式
//$conn=new Mongo(); #连接本地主机,默认端口.
//$conn=new Mongo(“172.21.15.69″); #连接远程主机
//$conn=new Mongo(“xiaocai.loc:10086″); #连接指定端口远程主机
//$conn=new Mongo(“xiaocai.loc”,array(“replicaSet”=>true)); #负载均衡
//$conn=new Mongo(“xiaocai.loc”,array(“persist”=>”t”)); #持久连接
//$conn=new Mongo(“mongodb://sa:123@localhost”); #带用户名密码
//$conn=new Mongo(“mongodb://localhost:27017,localhost:27018″); #连接多个服务器
//$conn=new Mongo(“mongodb:///tmp/mongo-27017.sock”); #域套接字
//$conn=new Mongo(“mongodb://admin_miss:miss@localhost:27017/test”,array(‘persist’=>’p',”replicaSet”=>true)); #完整
  2,选择数据库与表
  $db=$conn->mydb; #选择mydb数据库
  $collection=$db->myTable; #选择集合(选择’表’)
  //$collection=$db->selectCollection(myTable); #第二种写法
  //$collection=$conn->mydb->myTable; #更简洁的写法
  3,插入数据记录
$array=array(‘column_name’=>’col’.rand(100,999),’column_exp’=>’xiaocai’);
$result=$collection->insert($array); #简单插入
echo “新记录ID:”.$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:bool(true)
//**向集合中安全插入数据,返回插入状态(数组). **/
$array=array(‘column_name’=>’col’.rand(100,999),’column_exp’=>’xiaocai2′);
$result=$collection->insert($array,true); #用于等待MongoDB完成操作,以便确定是否成功.(当有大量记录插入时使用该参数会比较有用)
echo “新记录ID:”.$array['_id']; #MongoDB会返回一个记录标识
var_dump($result); #返回:array(3) { ["err"]=> NULL ["n"]=> int(0) ["ok"]=> float(1) }
  4,更新数据记录
//** 修改更新 传统更新 **/
$where=array(‘column_name’=>’col123′);
$newdata=array(‘column_exp’=>’GGGGGGG’,'column_fid’=>444);
$result=$collection->update($where,array(‘$set’=>$newdata)); #$set:让某节点等于给定值,类似的还有$pull $pullAll $pop $inc,在后面慢慢说明用法
/*
* 结果:
* 原数据
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col123″,”column_exp”:”xiaocai”}
* 被替换成了
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col123″,”column_exp”:”GGGGGGG”,”column_fid”:444}
*/
//** 替换更新 ,覆盖原记录**/
$where=array(‘column_name’=>’col709′);
$newdata=array(‘column_exp’=>’HHHHHHHHH’,'column_fid’=>123);
$result=$collection->update($where,$newdata);
/*
* 结果:
* 原数据
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_name”:”col709″,”column_exp”:”xiaocai”}
* 被替换成了
* {“_id”:ObjectId(“4d635ba2d549a02801000003″),”column_exp”:”HHHHHHHHH”,”column_fid”:123}
*/
//** 批量更新 **/
$where=array(‘column_name’=>’col’);
$newdata=array(‘column_exp’=>’multiple’,’91u’=>684435);
$result=$collection->update($where,array(‘$set’=>$newdata),array(‘multiple’=>true));
/**
* 所有’column_name’='col’都被修改
*/
//** 自动累加 **/
$where=array(’91u’=>684435);
$newdata=array(‘column_exp’=>’edit’);
$result=$collection->update($where,array(‘$set’=>$newdata,’$inc’=>array(’91u’=>-5)));
/**
* 更新91u=684435的数据,并且91u自减5
*/

PHP操作MongoDB 数据库总结记录

 5,删除记录操作
//** 删除节点 **/
$where=array(‘column_name’=>’col685′);
$result=$collection->update($where,array(‘$unset’=>’column_exp’));
/**
* 删除节点column_exp
*/
/*
* *
* 完整格式:update(array $criteria, array $newobj [, array $options = array()  ] )
*       注意:1.注意区分替换更新与修改更新
*    2.注意区分数据类型如 array(’91u’=>’684435′)与array(’91u’=>684435)
* *
*/
//** 清空表 **/
$collection->remove(); #清空集合
//** 删除指定MongoId **/
$id = new MongoId(“4d638ea1d549a02801000011″);
$collection->remove(array(‘_id’=>(object)$id));
  6,查询数据记录
//** 统计表记录数 **/
echo ‘count:’.$collection->count().”<br>”; #全部
echo ‘count:’.$collection->count(array(‘type’=>’user’)).”<br>”; #统计‘type’ 为 user 的记录
echo ‘count:’.$collection->count(array(‘age’=>array(‘$gt’=>50,’$lte’=>74))).”<br>”; #统计大于50小于等于74
echo ‘count:’.$collection->find()->limit(5)->skip(0)->count(true).”<br>”; #获得实际返回的结果数
/**
* 注:$gt为大于、$gte为大于等于、$lt为小于、$lte为小于等于、$ne为不等于、$exists不存在
*/
//** 获取表中所有记录 **/
$cursor = $collection->find()->snapshot();
foreach ($cursor as $id => $value) {
echo “$id: “; var_dump($value); echo “<br>”;
}
/**
* 注意:
* 在我们做了find()操作,获得$cursor游标之后,这个游标还是动态的.
* 换句话说,在我find()之后,到我的游标循环完成这段时间,如果再有符合条件的记录被插入到collection,那么这些记录也会被$cursor 获得.
* 如果你想在获得$cursor之后的结果集不变化,需要这样做:
* $cursor = $collection->find();
*/
//** 查询一条数据 **/
$cursor = $collection->findOne();
/**
*  注意:findOne()获得结果集后不能使用snapshot(),fields()等函数;
*/
//** 设置显示字段 age,type 列不显示 **/
$cursor = $collection->find()->fields(array(“age”=>false,”type”=>false));
$cursor = $collection->find()->fields(array(“user”=>true)); //只显示user 列
/**
* 我这样写会出错:$cursor->fields(array(“age”=>true,”type”=>false));
*/
//** 设置条件 (存在type,age节点) and age!=0 and age<50 **/
$where=array(‘type’=>array(‘$exists’=>true),’age’=>array(‘$ne’=>0,’$lt’=>50,’$exists’=>true));
$cursor = $collection->find($where);
//** 分页获取结果集  **/
$cursor = $collection->find()->limit(5)->skip(0);
//** 排序  **/
$cursor = $collection->find()->sort(array(‘age’=>-1,’type’=>1)); ##1表示降序 -1表示升序,参数的先后影响排序顺序
//** 索引  **/
$collection->ensureIndex(array(‘age’ => 1,’type’=>-1)); #1表示降序 -1表示升序
$collection->ensureIndex(array(‘age’ => 1,’type’=>-1),array(‘background’=>true)); #索引的创建放在后台运行(默认是同步运行)
$collection->ensureIndex(array(‘age’ => 1,’type’=>-1),array(‘unique’=>true)); #该索引是唯一的
/**
* ensureIndex (array(),array(‘name’=>’索引名称’,'background’=true,’unique’=true))
* 详见:http://www.php.net/manual/en/mongocollection.ensureindex.php
*/
//** 取得查询结果 **/
$cursor = $collection->find();
$array=array();
foreach ($cursor as $id => $value) {
$array[]=$value;
}
  7.关闭链接
  $conn->close(); #关闭连接
  8,常见函数使用
  a.$inc 如果记录的该节点存在,让该节点的数值加N;如果该节点不存在,让该节点值等于N
  设结构记录结构为 array(’a’=>1,’b’=>’t’),想让a加5,那么:
  $coll->update(array(’b’=>’t’),array(’$inc’=>array(’a’=>5)))
  b.$set 让某节点等于给定值
  设结构记录结构为 array(’a’=>1,’b’=>’t’),b为加f,那么:
  $coll->update(array(’a’=>1),array(’$set’=>array(’b’=>’f’)))
  c.$unset 删除某节点
  设记录结构为 array(’a’=>1,’b’=>’t’),想删除b节点,那么:
  $coll->update(array(’a’=>1),array(’$unset’=>’b’))
  d.$push 如果对应节点是个数组,就附加一个新的值上去;不存在,就创建这个数组,并附加一个值在这个数组上;如果该节点不是数组,返回错误。
  设记录结构为array(’a’=>array(0=>’haha’),’b’=& gt;1),想附加新数据到节点a,那么:
  $coll->update(array(’b’=>1),array(’$push’=>array(’a’=>’wow’)))
  这样,该记录就会成为:array(’a’=>array(0=>’haha’,1=>’wow’),’b’=>1)
  e.$pushAll 与$push类似,只是会一次附加多个数值到某节点
  f.$addToSet 如果该阶段的数组中没有某值,就添加之
  设记录结构为array(’a’=>array(0=& gt;’haha’),’b’=>1),如果想附加新的数据到该节点a,那么:
  $coll->update(array(’b’=>1),array(’$addToSet’=>array(’a’=>’wow’)))
  如果在a节点中已经有了wow,那么就不会再添加新的,如果没有,就会为该节点添加新的item——wow。
  g.$pop 设该记录为array(’a’=>array(0=>’haha’,1=>’wow’),’b’=>1)
  删除某数组节点的最后一个元素:
  $coll->update(array(’b’=>1),array(’$pop=>array(’a’=>1)))
  删除某数组阶段的第一个元素
  $coll->update(array(’b’=>1),array(’$pop=>array(’a’=>-1)))
  h.$pull 如果该节点是个数组,那么删除其值为value的子项,如果不是数组,会返回一个错误。
  设该记录为 array(’a’=>array(0=>’haha’,1=>’wow’),’b’=>1),想要删除a中value为 haha的子项:
  $coll->update(array(’b’=>1),array(’$pull=>array(’a’=>’haha’)))
  结果为: array(’a’=>array(0=>’wow’),’b’=>1)
  i.$pullAll 与$pull类似,只是可以删除一组符合条件的记录。

posted on 2014-05-29 11:22 顺其自然EVO 阅读(2059) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜