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

首頁 > 開發 > PHP > 正文

php實現的mongodb操作類

2024-05-04 23:35:39
字體:
來源:轉載
供稿:網友

說到php連mongoDB,不得不先介紹一下php的官方手冊,網址在:http://us.php.net/manual/en/book.mongo.php,接下來給大家分享一個本人常用的MONGODB的操作類,詳見的數據庫操作都有了,小伙伴可以參考下。

mongo_db.php

 

 
  1. <?php 
  2.  
  3. /** 
  4. * Created by PhpStorm. 
  5. * User: yangyulong 
  6. * Date: 2015/5/26 
  7. * Time: 13:45 
  8. */ 
  9. class Mongo_db 
  10. private static $instanceof = NULL; 
  11. public $mongo
  12. private $host = 'localhost'
  13. private $port = '27017'
  14.  
  15. private $db
  16. public $dbname
  17. private $table = NULL; 
  18.  
  19. /** 
  20. * 初始化類,得到mongo的實例對象 
  21. */ 
  22. public function __construct($host = NULL, $port = NULL, $dbname = NULL, $table = NULL) 
  23.  
  24. if (NULL === $dbname) { 
  25. $this->throwError('集合不能為空!'); 
  26.  
  27. //判斷是否傳遞了host和port 
  28. if (NULL !== $host) { 
  29. $this->host = $host
  30.  
  31. if (NULL !== $port) { 
  32. $this->port = $port
  33.  
  34. $this->table = $table
  35.  
  36. $this->mongo = new MongoClient($this->host . ':' . $this->port); 
  37. if ($this->getVersion() >= '0.9.0') { 
  38. $this->dbname = $this->mongo->selectDB($dbname); 
  39. $this->db = $this->dbname->selectCollection($table); 
  40. else { 
  41. $this->db = $this->mongo->$dbname->$table
  42.  
  43. public function getVersion() 
  44. return MongoClient::VERSION; 
  45.  
  46. /** 
  47. * 單例模式 
  48. * @return Mongo|null 
  49. */ 
  50. //public static function getInstance($host=null, $port=null, $dbname=null, $table=null){ 
  51. // 
  52. // if(!(self::$instanceof instanceof self)){ 
  53. // self::$instanceof = new self($host, $port, $dbname, $table); 
  54. // } 
  55. // 
  56. // return self::$instanceof; 
  57. //} 
  58.  
  59. /** 
  60. * 插入一條數據 
  61. * @param array $doc 
  62. */ 
  63. public function insert($doc = array()) 
  64. if (emptyempty($doc)) { 
  65. $this->throwError('插入的數據不能為空!'); 
  66. //保存數據信息 
  67. try { 
  68. if (!$this->db->insert($doc)) { 
  69. throw new MongoException('插入數據失敗'); 
  70. } catch (MongoException $e) { 
  71. $this->throwError($e->getMessage()); 
  72.  
  73. /** 
  74. * 插入多條數據信息 
  75. * @param array $doc 
  76. */ 
  77. public function insertMulti($doc = array()) 
  78. if (emptyempty($doc)) { 
  79. $this->throwError('插入的數據不能為空!'); 
  80. //插入數據信息 
  81. foreach ($doc as $key => $val) { 
  82. //判斷$val是不是數組 
  83. if (is_array($val)) { 
  84. $this->insert($val); 
  85.  
  86. /** 
  87. * 查找一條記錄 
  88. * @return array|null 
  89. */ 
  90. public function findOne($where = NULL) 
  91. if (NULL === $where) { 
  92. try { 
  93. if ($result = $this->db->findOne()) { 
  94. return $result
  95. else { 
  96. throw new MongoException('查找數據失敗'); 
  97. } catch (MongoException $e) { 
  98. $this->throwError($e->getMessage()); 
  99. else { 
  100. try { 
  101. if ($result = $this->db->findOne($where)) { 
  102. return $result
  103. else { 
  104. throw new MongoException('查找數據失敗'); 
  105. } catch (MongoException $e) { 
  106. $this->throwError($e->getMessage()); 
  107.  
  108.  
  109. /** 
  110. * todo 帶條件的隨后做 
  111. * 查找所有的文檔 
  112. * @return MongoCursor 
  113. */ 
  114. public function find($where = NULL) 
  115. if (NULL === $where) { 
  116.  
  117. try { 
  118. if ($result = $this->db->find()) { 
  119.  
  120. else { 
  121. throw new MongoException('查找數據失敗'); 
  122. } catch (MongoException $e) { 
  123. $this->throwError($e->getMessage()); 
  124. else { 
  125. try { 
  126. if ($result = $this->db->find($where)) { 
  127.  
  128. else { 
  129. throw new MongoException('查找數據失敗'); 
  130. } catch (MongoException $e) { 
  131. $this->throwError($e->getMessage()); 
  132.  
  133. $arr = array(); 
  134. foreach ($result as $id => $val) { 
  135. $arr[] = $val
  136.  
  137. return $arr
  138.  
  139. /** 
  140. * 獲取記錄條數 
  141. * @return int 
  142. */ 
  143. public function getCount() 
  144. try { 
  145. if ($count = $this->db->count()) { 
  146. return $count
  147. else { 
  148. throw new MongoException('查找總數失敗'); 
  149. } catch (MongoException $e) { 
  150. $this->throwError($e->getMessage()); 
  151.  
  152. /** 
  153. * 獲取所有的數據庫 
  154. * @return array 
  155. */ 
  156. public function getDbs() 
  157. return $this->mongo->listDBs(); 
  158.  
  159. /** 
  160. * 刪除數據庫 
  161. * @param null $dbname 
  162. * @return mixed 
  163. */ 
  164. public function dropDb($dbname = NULL) 
  165. if (NULL !== $dbname) { 
  166. $retult = $this->mongo->dropDB($dbname); 
  167. if ($retult['ok']) { 
  168. return TRUE; 
  169. else { 
  170. return FALSE; 
  171. $this->throwError('請輸入要刪除的數據庫名稱'); 
  172.  
  173. /** 
  174. * 強制關閉數據庫的鏈接 
  175. */ 
  176. public function closeDb() 
  177. $this->mongo->close(TRUE); 
  178.  
  179. /** 
  180. * 輸出錯誤信息 
  181. * @param $errorInfo 錯誤內容 
  182. */ 
  183. public function throwError($errorInfo=''
  184. echo "<h3>出錯了:$errorInfo</h3>"
  185. die(); 
  186.  

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌珠穆沁旗| 东阿县| 湘乡市| 堆龙德庆县| 榆林市| 水富县| 金秀| 苏尼特右旗| 合作市| 怀仁县| 鸡泽县| 衢州市| 新闻| 内黄县| 碌曲县| 利津县| 北宁市| 伊宁县| 定陶县| 奇台县| 大埔区| 余干县| 和龙市| 垣曲县| 和顺县| 隆子县| 玉龙| 吴旗县| 闻喜县| 灵台县| 晋宁县| 滕州市| 双桥区| 丹阳市| 清新县| 梅河口市| 互助| 合水县| 衢州市| 资阳市| 沅江市|