MongoDB數據庫常用操作PHP代碼

pw36 9年前發布 | 8K 次閱讀 PHP MongoDB

<?php

$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”); #帶用戶名密碼

選擇test數據庫

$db=$conn->test; //$db=$conn->selectDB("test"); #第二種寫法

選擇集合(選擇"表")

$collection=$db->user; //$collection=$db->selectCollection("user"); #第二種寫法

插入操作

$data=array("uid"=>"zz123","user_name"=>"張三"); $result=$collection->insert($data); #簡單插入 echo "插入數據的id".$data["_id"];

exit;

插入操作 安全插入

$data=array("uid"=>"zz124","user_name"=>"李四"); $result=$collection->insert($data,true); #用于等待MongoDB完成操作,以便確定是否成功.(當有大量記錄插入時使用該參數會比較有用)

修改操作

$where=array("uid"=>"zz123"); $newdata=array("user_name"=>"張三三","tel"=>"123456789"); $result=$collection->update($where,array('$set'=>$newdata));

替換更新

$where=array("uid"=>"zz124"); $newdata=array("user_age"=>"22","tel"=>"123456789"); $result=$collection->update($where,$newdata);

批量更新

$where=array("uid"=>'zz'); $newdata=array("user_name"=>"zz","money"=>1000); $result=$collection->update($where,array('$set'=>$newdata),array('multiple'=>true));

自動累加

$where=array('money'=>1000); $newdata=array('user_name'=>'edit'); $result=$collection->update($where,array('$set'=>$newdata,'$inc'=>array('money'=>-5)));

刪除節點

$where=array('uid'=>'zz124'); $result=$collection->update($where,array('$unset'=>'tel'));//刪除節點tel

刪除數據

$collection->remove(array('uid'=>'zz124'));

刪除指定MongoId

$id = new MongoId('4d638ea1d549a02801000011'); $collection->remove(array('_id'=>(object)$id));

查詢數據 注:$gt為大于、$gte為大于等于、$lt為小于、$lte為小于等于、$ne為不等于、$exists不存在

echo 'count:'.$collection->count()."<br>"; #全部 echo 'count:'.$collection->count(array('uid'=>'zz123'))."<br>"; #可以加上條件 echo 'count:'.$collection->count(array('age'=>array('$gt'=>10,'$lte'=>30)))."<br>"; #大于50小于等于74 echo 'count:'.$collection->find()->limit(5)->skip(0)->count(true)."<br>"; #獲得實際返回的結果數

集合中所有文檔

$cursor = $collection->find()->snapshot(); foreach ($cursor as $id => $value) { echo "$id: "; var_dump($value); echo "<br>"; }

查詢一條數據

$cursor = $collection->findOne();

排除列 false為不顯示

$cursor = $collection->find()->fields(array("age"=>false,"tel"=>false));

指定列 true為顯示

$cursor = $collection->find()->fields(array("user_name"=>true));

(存在tel,age節點) and age!=0 and age<50

$where=array('tel'=>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,'money'=>-1)); #1表示降序 -1表示升序 $collection->ensureIndex(array('age' => 1,'money'=>-1),array('background'=>true)); #索引的創建放在后臺運行(默認是同步運行) $collection->ensureIndex(array('age' => 1,'money'=>-1),array('unique'=>true)); #該索引是唯一的

取得查詢結果

$cursor = $collection->find(); $array=array(); foreach ($cursor as $id => $value) { $array[]=$value; }

關閉連接

$conn->close(); ?></pre>

 本文由用戶 pw36 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!