PHP操作MongoDB
原文: http://www.cnblogs.com/jackluo/archive/2013/06/16/3138835.html
//************************* // // // / $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) } / #insert($array,array(‘safe’=>false,’fsync’=>false,’timeout’=>10000)) //************************* // / $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,在后面慢慢说明用法 / $where=array(‘column_name’=>’col709′); $newdata=array(‘column_exp’=>’HHHHHHHHH’,'column_fid’=>123); $result=$collection->update($where,$newdata); //** 批量更新 **/ $where=array(‘column_name’=>’col’); $newdata=array(‘column_exp’=>’multiple’,’91u’=>684435); $result=$collection->update($where,array(‘$set’=>$newdata),array(‘multiple’=>true)); //** 自动累加 **/ $where=array(’91u’=>684435); $newdata=array(‘column_exp’=>’edit’); $result=$collection->update($where,array(‘$set’=>$newdata,’$inc’=>array(’91u’=>-5))); $where=array(‘column_name’=>’col685′); $result=$collection->update($where,array(‘$unset’=>’column_exp’)); //************************* // / $collection->remove(array(‘column_name’=>’col399′)); //$collection->remove(); #清空集合 $id = new MongoId(“4d638ea1d549a02801000011″); $collection->remove(array(‘_id’=>(object)$id)); //************************* // / echo ‘count:’.$collection->count().”
”; #全部 echo ‘count:’.$collection->count(array(‘type’=>’user’)).”
”; #可以加上条件 echo ‘count:’.$collection->count(array(‘age’=>array(‘$gt’=>50,’$lte’=>74))).”
”; #大于50小于等于74 echo ‘count:’.$collection->find()->limit(5)->skip(0)->count(true).”
”; #获得实际返回的结果数 $cursor = $collection->find()->snapshot(); foreach ($cursor as $id => $value) { echo “$id: “; var_dump($value); echo “
”; } $cursor = $collection->findOne(); $cursor = $collection->find()->fields(array(“age”=>false,”type”=>false)); $cursor = $collection->find()->fields(array(“user”=>true)); $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)); #该索引是唯一的 $cursor = $collection->find(); $array=array(); foreach ($cursor as $id => $value) { $array[]=$value; } //************************* // /
什么是