国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP操作Mongodb封裝類完整實例

2024-05-05 00:04:26
字體:
來源:轉載
供稿:網友

本文實例講述了PHP操作Mongodb封裝類。分享給大家供大家參考,具體如下:

<?php/*** Mongodb 基本操作API,支持基本類似關系統型數據庫的操作接口** @version 1.0 * [說明] * * 1:該版本API實現了 Mongodb 中最基本的插入/修改/查詢/刪除操作的封裝 * 2:其它更高級的操作可通過 $this->getMongo() 得到原生的對象,更多API請自行查閱 Mongo PHP手冊,后續版本將會對增加更多的原生API封裝 * 3:該類所有API接口中的 $query 查詢參數的使用請以下有關 [查詢條件說明文檔] * 4: 如果要存儲中文字符,則一定要使用 utf8 的編碼. * 5:有了本類接口基本可以按關系型數據庫的概念完成Mongodb的大部分開發操作。 * * [查詢條件說明文檔] * * 參數:array('id'=>1) * 等同:where id=1 * * 參數:array('id'=>1,'num'=>10) * 等同:where id=1 and num=10 * * 參數:array('id'=>array($mongo->cmd('>')=>5)) * 等同:where id>5 * * 參數:array('id'=>array($mongo->cmd('!=')=>5)) * 等同:where id!=5 * * 參數:array('id'=>array($mongo->cmd('>')=>5, $mongo->cmd('<')=>10)) * 等同:where id>5 and id<10 * * 參數:array('id'=>array($mongo->cmd('in')=>array(2,5,6))) * 等同:where id in (2,5,6) * * 參數:array('id'=>array($mongo->cmd('%')=>array(2,1))) * 等同:where id % 2 = 1 * * 參數:array($mongo->cmd('or') => array( array('id'=>array($mongo->cmd('>')=>5)), array('id'=>array($mongo->cmd('<')=>10)) ) ) * 等同:where id>5 or id<10 * * 參數:array('username' => new mongoRegex("/yhp.*/"))  * 等同 where username like "%yhp%" **/class Library_Mongodb {  /**   * Mongodb 對象句柄   *   * @var object Mongo   */  private $_mongo = null;  /**   * 當前選擇的數據庫   *   * @var object MongoDB   */  private $_db = null;  /**   * 修改器命令前綴   *   * @var string   */  private $_cmd = '$';  /**   * 調試模式 TRUE 打開 FALSE 關閉   * @var boolean   */  const DEBUG = TRUE;  /**   * 查詢條件映射關系   *   * @var array   */  private $_condMap = array(    '<'    =>  'lt', // id > 1    '<='  =>  'lte', // id <= 10    '>'    =>  'gt', // id > 5    '>='  =>  'gte', // id >= 4    '!='  =>  'ne', // id != 4    '%'    =>  'mod', // id % 4 = 0    'in'  =>  'in', // id in (1,2,3,4)    'notin'  =>  'nin',// id not in (1,2,3,4)    'or'  =>  'or', // id=1 or id=2    'not'  =>  'not', // !(id=1)  );  /**   * 構造函數   *   * @param array $config 服務器配置,默認為:   * array(   * 'host'=>'localhost', // 主機名或IP地址   * 'port'=>27017, // 端口   * 'cmd'=>'$', // 修改器命令前綴   * )   */  public function __construct($config = array('host' => 'xxx', 'port' => 27017, 'username' => 'xxx', 'password' => 'xxx', 'db' => 'xxx', 'cmd' => '$')){    $server = sprintf("mongodb://%s:%s@%s:%s/%s", $config['username'], $config['password'], $config['host'], $config['port'], $config['db']);//    echo "connect/n";    try {      $this->_mongo = new MongoClient($server, array('connect'=>true));// 立即連接    }catch (MongoConnectionException $e){      if(self::DEBUG) {        echo $e->getMessage();      }      return false;    }    $this->selectDB($config['db']);    // 命令前綴    if(!isset($config['cmd'])){      $this->_cmd = ini_get('mongo.cmd');      if($this->_cmd == ''){        $this->_cmd = '$';      }    }  }  /* ==================================== 基本操作接口API ================================= */  /**   * 向集合(表)中插入新文檔   *   * 說明:   * 1:類似mysql中的: insert into $colName set id=1,name='name1';   *   * @param string $colName 集合名   * @param array $sets 數據,如: array('id'=>1,'name'=>'name1')   * @param boolean $safe 是否安全操作 false:不等待服務器的響應直接返回 true:等待服務器的響應(數據非常重要時推薦)   * @param boolean $fsync 操作后是否立即更新到碰盤,默認情況下由服務器決定   *   * @return boolean   */  public function insert($colName, $sets, $safe=false, $fsync=false){    $col = $this->_getCol($colName);    try {      $col->insert($sets,array('w'=>$safe,'fsync'=>$fsync));      return true;    }catch (MongoCursorException $e){      return false;    }  }  /**   * 保存文檔   *   * 說明:   * 1:如果 $sets 中有字段 "_id" 的話,則更新對應的文檔;否則插入新文檔   *   * @param string $colName 集合名   * @param array $sets 數據,如: array('id'=>1,'name'=>'name1')   * @param boolean $safe 是否安全操作 false:不等待服務器的響應直接返回 true:等待服務器的響應(數據非常重要時推薦)   * @param boolean $fsync 操作后是否立即更新到碰盤,默認情況下由服務器決定   *   * @return boolean   */  public function save($colName, $sets, $safe=false, $fsync=false){    // 處理 '_id' 字段    $sets = $this->_parseId($sets);    $ret = $this->_getCol($colName)->save($sets,array('w'=>$safe,'fsync'=>$fsync));    return $ret;  }  /**   * 刪除集合中的文檔記錄   *   * 說明:   * 1:類似mysql中的: delete from $colName where id=1;   *   * @param string $colName 集合名   * @param array $query 查詢條件,如果為空數組的話,則會刪除所有記錄.具體請看 [查詢條件說明文檔]   * @param boolean $delAll 是否刪除所以條例查詢的記錄,默認為 true,當為 false是,類似效果 delete from tab where id=1 limit 1;   * @param boolean $safe 是否安全操作 false:不等待服務器的響應直接返回 true:等待服務器的響應(數據非常重要時推薦)   * @param boolean $fsync 操作后是否立即更新到碰盤,默認情況下由服務器決定   *   * @return boolean   */  public function delete($colName,$query=array(),$delAll=true,$safe=false,$fsync=false){    // 自動處理 '_id' 字段    $query = $this->_parseId($query);    // 刪除選項    $options = array(      'justOne'  =>  !$delAll,      'w'      =>  $safe,      'fsync'    =>  $fsync,    );    $col = $this->_getCol($colName);    return $col->remove($query,$options);  }  /**   * 刪除整個集合   *   * 說明:   * 1:集合中的索引也會被刪除   *   * @param string $colName 集合名   *   * @return array   */  public function dropCol($colName){    return $this->_getCol($colName)->drop();  }  /**   * 更新集合文檔記錄   *   * 說明:   * 1:類似mysql中的: update $colName set name='mongo' where id=10;   *   * @param string $colName 集合名   * @param array $newDoc 要更新的文檔記錄   * @param array $query 查詢條件,如果為空數組則更新所有記錄.具體請看 [查詢條件說明文檔]   * @param string $option 操作選項,可選擇項如下;   *   * 'set':只修改指定的字段(默認值,如果這個鍵不存在,則創建它。存在則更新).   * 示例: update('user', array('name'=>'mongo'), array('id'=>10));   * 類似: update user set name='mongo' where id=10;   *   * 'inc':將指定的字段累加/減(如果值為負數則是相減,不存在鍵則創建。字段類型一定要是數字)   * 示例:update('user', array('num'=>1), array('id'=>10), 'inc');   * 類似: update user set num=num+1 where id=10;   *   * 'push':將文檔添加到指定鍵中(數組),如果鍵不存在則會自動創建,存在則添加到該鍵的尾端。   * 示例:update('user', array('comm'=>array('commid'=>1,'title'=>'title1')), array('id'=>1), 'push');   * 解說:為 id=1 的記錄添加一個 comm 的評論字段,該字段對應一個 array('commid'=>1,'title'=>'title1') 的新文檔。   *   * 'pop':將指定鍵中的文檔刪除(數組)   * 示例:update('user', array('comm'=>array('commid'=>1)), array('id'=>1), 'pop');   * 解說:刪除 id=1 的記錄中 comm 對應的文檔集合中 'commid'=>1 對應的文檔.   *   * 'unset':在文檔中刪除指定的鍵   * 示例:update('user', array('name'=>1), array('id'=>1), 'unset');   * 解說: 將 user 集合中將 id=1 對應的文檔中的 name 字段刪除   *   * 'pull':刪除文檔中匹配其值的鍵   * 示例:update('user', array('name'=>'youname'), array('id'=>1), 'pull');   * 解說:將 user 集合中將 id=1 對應的文檔中的 name='youname' 的字段刪除   *   * 'addToSet':如果值不存在就添加(避免重復添加)   * 示例:update('user', array('names'=>'youname'), array('id'=>1), 'addToSet');   * 解說:向 user 集合中 id=1 對應的文檔中的 names 字段添加 'youname' 這個值(不存在時才添加)   *   * 'replace':用 $newDoc 新文檔替換 $query 所找到的文檔   * 示例:update('user', array('newid'=>1,'newnames'=>'name1'), array('id'=>1), 'replace');   * 解說:將 user 集合中 id=1 對應的文檔用 array('newid'=>1,'newnames'=>'name1') 的新文檔替換   *   * @param boolean $upAll 是否更新找到的所有記錄   * @param boolean $upsert 如果查詢條件不存在時,是否以查詢條件和要更新的字段一起新建一個集合   * @param boolean $safe 是否安全刪除 false:不等待服務器的響應直接返回 true:等待服務器的響應(數據非常重要時推薦)   * @param boolean $fsync 操作后是否立即更新到碰盤,默認情況下由服務器決定   *   * @return boolean   */  public function update($colName,$newDoc,$query=array(),$option='set',$upAll=true,$upsert=false,$safe=false,$fsync=false){    // 自動處理 '_id' 字段    $query = $this->_parseId($query);    // 得到集合    $col = $this->_getCol($colName);    // 重新組合新文檔    if($option != 'replace'){      $newDoc = array($this->cmd($option) => $newDoc);    }    // 更新條件    $options = array(      'upsert'  =>  $upsert,      'multiple'  =>  $upAll,      'w'      =>  $safe,      'fsync'    =>  $fsync,    );    return $col->update($query,$newDoc,$options);  }  /**   * 查詢文檔集,返回二維數組   *   * 說明:   * 1:類似mysql中的 select * from table   *   * 示例:select('user');   * 類似:select * from user;   *   * 示例:select('user',array('id','name'));   * 類似:select id,name from user;   *   * 示例:select('user',array('id','name'),array('id'=>1));   * 類似:select id,name from user where id=1;   *   * 示例:select('user',array('id','name'),array('id'=>1),array('num'=>1));   * 類似:select id,name from user where id=1 order by num asc;   *   * 示例:select('user',array('id','name'),array('id'=>1),array('num'=>1),10);   * 類似:select id,name from user where id=1 order by num asc limit 10;   *   * 示例:select('user',array('id','name'),array('id'=>1),array('num'=>1),10,2);   * 類似:select id,name from user where id=1 order by num asc limit 2,10;   *   *   *   * @param string $colName 集合名   * @param array $query 查詢條件,具體請看 [查詢條件說明文檔]   * @param array $fields 結果集返回的字段, array():表示返回所有字段 array('id','name'):表示只返回字段 "id,name"   * @param array $sort 排序字段, array('id'=>1):表示按id字段升序 array('id'=>-1):表示按id字段降序 array('id'=>1, 'age'=>-1):表示按id升序后再按age降序   * @param int $limit 取多少條記錄   * @param int $skip 跳過多少條(從多少條開始)   *   * @return array   */  public function select($colName,$query=array(),$fields=array(),$sort=array(),$limit=0,$skip=0){    // 得到集合    $col = $this->_getCol($colName);    // 自動處理 '_id' 字段    $query = $this->_parseId($query);    // 結果集偏歷    $cursor = $col->find($query,$fields);    // 排序    if($sort){      $cursor->sort($sort);    }    // 跳過記錄數    if($skip > 0){      $cursor->skip($skip);    }    // 取多少行記錄    if($limit > 0){      $cursor->limit($limit);    }    $result = array();    foreach($cursor as $row){      $result[] = $this->_parseArr($row);    }    return $result;  }  /**   * 統計文檔記錄數   *   * @param string $colName 集合名   * @param array $query 查詢條件,具體請看 [查詢條件說明文檔]   * @param int $limit 取多少條記錄   * @param int $skip 跳過多少條   * @return unknown   */  public function count($colName,$query=array(),$limit=0,$skip=0){    return $this->_getCol($colName)->count($query,$limit,$skip);  }  /**   * 返回集合中的一條記錄(一維數組)   *   * @param string $colName 集合名   * @param array $query 查詢條件,具體請看 [查詢條件說明文檔]   * @param array $fields 結果集返回的字段, array():表示返回所有字段 array('id','name'):表示只返回字段 "id,name"   *   * @return array   */  public function fetchRow($colName,$query=array(), $fields=array()){    // 得到集合名    $col = $this->_getCol($colName);    // 自動處理 '_id' 字段    $query = $this->_parseId($query);    // 處理結果集    return $this->_parseArr($col->findOne($query,$fields));  }  /**   * 返回符合條件的文檔中字段的值   *   * @param string $colName 集合名   * @param array $query 查詢條件,具體請看 [查詢條件說明文檔]   * @param string $fields 要取其值的字段,默認為 "_id" 字段,類似mysql中的自增主鍵   *   * @return mixed   */  public function fetchOne($colName,$query=array(), $fields='_id'){    $ret = $this->fetchRow($colName,$query,array($fields));    return isset($ret[$fields]) ? $ret[$fields] : false;  }  /**   * 返回查詢文檔集合集中指定字段的值(一維數組)   *   * @param string $colName 集合名   * @param array $query 查詢條件,具體請看 [查詢條件說明文檔]   * @param string $fields 要取其值的字段,默認為 "_id" 字段,類似mysql中的自增主鍵   *   * @return array   */  public function fetchCol($colName,$query=array(), $fields='_id'){    $result = array();    $list = $this->select($colName,$query,array($fields));    foreach ($list as $row){      $result[] = $row[$fields];    }    return $result;  }  /**   * 返回指定下標的查詢文檔集合(二維數組)   *   * @param string $colName 集合名   * @param array $query 查詢條件,具體請看 [查詢條件說明文檔]   * @param string $fields 要取其值的字段,默認為 "_id" 字段,類似mysql中的自增主鍵   *   * @return array   */  public function fetchAssoc($colName,$query=array(), $fields='_id'){    $result = array();    $list = $this->select($colName,$query);    foreach ($list as $row){      $key = $row[$fields];      $result[][$key] = $row;    }    return $result;  }  /* ==================================== 輔助操作接口API ================================= */  /**   * 返回命令或命令前綴   *   * @param string $option 命令,如果為空時則返回命令前綴   *   * @return string   */  public function cmd($option=''){    // 只返回命令前綴    if($option == ''){      return $this->_cmd;    }    // 如果是操作符    if(isset($this->_condMap[$option])){      $option = $this->_condMap[$option];    }    return $this->_cmd.$option;  }  /**   * 選擇或創建數據庫(注意:新創建的數據庫如果在關閉連接前沒有寫入數據將會被自動刪除)   *   * @param string $dbname 數據庫名   */  public function selectDB($dbname){    $this->_db = $this->_mongo->selectDB($dbname);  }  /**   * 得到所有的數據庫   *   * @param boolean $onlyName 是否只返回數據庫名的數組   * @return array   */  public function allDB($onlyName=false){    $ary = $this->_mongo->listDBs();    if($onlyName){      $ret = array();      foreach ($ary['databases'] as $row){        $ret[] = $row['name'];      }      return $ret;    }else{      return $ary;    }  }  /**   * 刪除數據庫   *   * @return array   */  public function dropDB($dbname){    return $this->_mongo->dropDB($dbname);  }  /**   * 關閉連接   *   */  public function close(){    $this->_mongo->close();  }  /**   * 得到 Mongo 原生對象,進行其它更高級的操作,詳細請看PHP手冊   *   */  public function getMongo(){    return $this->_mongo;  }  /**   * 返回最后的錯誤信息   *   * @return array   */  public function getError(){    return $this->_db->lastError();  }  /* ======================= 以下為私有方法 ====================== */  // 解析數據組中的'_id'字段(如果有的話)  private function _parseId($arr){    if(isset($arr['_id'])){      $arr['_id'] = new MongoId($arr['_id']);    }    return $arr;  }  // 得到集合對象  private function _getCol($colName){    return $this->_db->selectCollection($colName);  }  // 解析數組中的"_id"并且返回  private function _parseArr($arr){    if(!empty($arr)) {      $ret = (array)$arr['_id'];      $arr['_id'] = $ret['$id'];    }    return $arr;  }}//End Class?>

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 昔阳县| 房产| 万载县| 垣曲县| 莆田市| 新平| 清水河县| 莱阳市| 钟祥市| 华安县| 韶山市| 如东县| 澳门| 千阳县| 陵川县| 遵化市| 凯里市| 巴彦淖尔市| 镇安县| 广宗县| 阿拉善盟| 洛川县| 阿瓦提县| 乌拉特后旗| 错那县| 会同县| 黄龙县| 榆林市| 孝昌县| 嘉祥县| 阳城县| 剑川县| 宝山区| 重庆市| 博兴县| 夹江县| 志丹县| 大姚县| 安溪县| 城步| 邻水|