基於php的mongodb類庫
阿新 • • 發佈:2018-12-18
第一次使用mongo的時候給我的感覺就是文件物件處理起來麻煩,查詢語法拼接不夠平滑好記,因為處理慣了陣列,所以這裡基於嘗試著以tp的方式封裝了一個類庫,方便使用
呼叫方式都在最下面已經註釋掉的部分,快來試試吧
<?php /** * mongodb驅動類 */ class MongoDriver { private static $db; private static $currentCollection; private $option = array( 'condition'=>array(), 'field'=>array('_id'=>false), 'sort'=>array(), 'limitNum'=>null, 'offset'=>null ); private static $_config = array( 'host'=>'127.0.0.1', 'port'=>'27017', 'dbName'=>'test', 'user'=>'wujia', 'pass'=>'wujia' ); public function __construct() { $this->connect(); } /** * [connect 連線方法] * @param array $connectInfo [連線資訊] */ public function connect($connectInfo=array()) { if(!$connectInfo) { $connectInfo = self::$_config; } $connectLink ="mongodb://".$connectInfo['host'].":".$connectInfo['port']; $mongo = new MongoClient($connectLink); // $mongo = new MongoDB\Driver\Manager($connectLink); $db = $mongo->$connectInfo['dbName']; $db->authenticate($connectInfo['user'], $connectInfo['pass']); self::$db = $db; } /** * [collection 選擇集合] * @param [string] $collectionName [集合名稱] */ public function collection($collectionName) { if($collectionName&&strlen($collectionName)>0) { self::$currentCollection = self::$db->$collectionName; } } /** * [variableInit option內參數初始化] */ private function variableInit() { $this->option =array( 'condition'=>array(), 'field'=>array('_id'=>false), 'sort'=>array(), 'limitNum'=>null, 'offset'=>null ); } /** * [where 條件組織方法] * @param array/string $condition [篩選條件,支援字串/陣列] */ public function where($condition) { //數字形式且不為空 if(count($condition)>0) { $this->option['condition'] = $condition; } //字串形式 if(is_string($condition)&&strlen($condition)>0) { $cond = array(); //小寫 $condition = strtolower($condition); $condition = explode('and',$condition); //and 條件組織 foreach($condition as $v) { list($key,$value) = explode('=',$v); $key = trim($key); $value = trim($value); //value是數字 if(is_numeric($value)) { if(strpos($value,'.')>0) { //浮點型 $value = floatval($value); } else { //整形 $value = intval($value); } } //value是字串,去掉'' if(is_string($value)) { $value = trim($value,"'"); } $cond[$key] = $value; } $this->option['condition'] = $cond; } return $this; } /** * [field 返回欄位組織方法] * @param [array/string] $fileds [引數列表,支援字串或陣列] */ public function field($fileds) { $fieldArr = array(); //字串形式 if(is_string($fileds)&&strlen($fileds)>0) { $fileds = strtolower($fileds); $fileds = explode(',',$fileds); } foreach($fileds as $value) { $fieldArr[$value]=true; } $this->option['field'] = array_merge($this->option['field'],$fieldArr); return $this; } /** * [add 資料寫入方法] * @param [array] $data [新增資料陣列] */ public function add($data) { if(is_array($data)&&count($data)>0) { return self::$currentCollection->insert($data); } } /** * [select 多條資料查詢方法] * @return [array] [查詢結果] */ public function select() { $result = self::$currentCollection ->find($this->option['condition'],$this->option['field']) ->sort($this->option['sort']) ->limit($this->option['limitNum']) ->skip($this->option['offset']); $returnArr = array(); foreach($result as $key => $value) { $returnArr[]=$value; } $this->variableInit(); return $returnArr; } /** * [find 獲取單條記錄方法] * @return [array] [查詢結果] */ public function find() { $result = self::$currentCollection->findOne($this->option['condition'],$this->option['field']); $this->variableInit(); return $result; } /** * [save 資料更新方法] * @param [array] $data [更新欄位陣列] * @return [boolean] [是否更新成功] */ public function save($data) { $update = self::$currentCollection->update($this->option['condition'],array('$set'=>$data)); $this->variableInit(); $re = $update['nModified']>0?true:false; return $re; } /** * [del 刪除方法] * @param boolean $limitOne [是否只刪除一條] * @return [type] [刪除結果] */ public function del($limitOne=false) { $justOne =false; if($limitOne == 1) { $justOne = true; } $re = self::$currentCollection->remove($this->option['condition'],array('justOne'=>$justOne)); $this->variableInit(); $re = $re['n']>0?true:false; return $re; } /** * [order 排序方法] * @param array $rules [排序規則] * @return [type] [description] */ public function order($rules = array()) { if(is_string($rules)&&strlen($rules)>0) { $rule = explode(',',strtolower($rules)); $rulesArr = array(); foreach($rule as $key => $value) { list($field,$sort)=explode(' ',$value); $sorted = $sort=='desc'?-1:1; $rulesArr[$field] = $sorted; } $this->option['sort'] = $rulesArr; } if(is_array($rules)&&count($rules)>0) { $this->option['sort'] = $rules; } return $this; } /** * [limit 分頁方法] * @param [integer] $offset [偏移量] * @param [integer] $limitNum [查詢數量] */ public function limit($offset,$limitNum=null) { $this->option['limitNum'] = is_null($limitNum)?$offset:$limitNum; $this->option['offset'] = is_null($limitNum)?0:$offset; return $this; } } //例項化 $mongodb = new MongoDriver(); //選擇集合 $mongodb->collection('user'); /* 新增資料 $data = array( 'name'=>'zhenghongying1', 'age'=>49 ); $re = $mongodb->add($data); if($re['ok']==1) { echo '插入成功'; } */ /* 查詢多條資料 $re = $mongodb->select(); $re = $mongodb->field("name,age")->where(array('name'=>'zhenghongying1'))->order(array("age"=>-1))->limit(2)->select(); 查詢1條資料 $re = $mongodb->field("name,age")->where(array('name'=>'zhenghongying1','age'=>49))->find(); */ /* 更新 $where = array("name"=>'zhenghongying1'); $data = array("age"=>19); $re =$mongodb->where($where)->save($data); */ /* 刪除 $re = $mongodb->where("name='zhenghongying1'")->del(1); */ $re = $mongodb ->field("name,age") ->where(array('name'=>'zhenghongying1')) ->order(array("age"=>-1)) ->limit(2) ->select(); var_dump($re);