亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 1761 | 回復(fù): 0
打印 上一主題 下一主題

php實(shí)現(xiàn)的mongodb操作類 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-07-20 13:06 |只看該作者 |倒序?yàn)g覽
mongo_db.php
  1. <?php

  2. /**
  3. * Created by PhpStorm.
  4. * User: yangyulong
  5. * Date: 2015/5/26
  6. * Time: 13:45
  7. */
  8. class Mongo_db
  9. {
  10.     private static $instanceof = NULL;
  11.     public $mongo;
  12.     private $host = 'localhost';
  13.     private $port = '27017';

  14.     private $db;
  15.     public $dbname;
  16.     private $table = NULL;

  17.     /**
  18.      * 初始化類,得到mongo的實(shí)例對(duì)象
  19.      */
  20.     public function __construct($host = NULL, $port = NULL, $dbname = NULL, $table = NULL)
  21.     {

  22.         if (NULL === $dbname) {
  23.             $this->throwError('集合不能為空!');
  24.         }

  25.         //判斷是否傳遞了host和port
  26.         if (NULL !== $host) {
  27.             $this->host = $host;
  28.         }

  29.         if (NULL !== $port) {
  30.             $this->port = $port;
  31.         }

  32.         $this->table = $table;

  33.         $this->mongo = new MongoClient($this->host . ':' . $this->port);
  34.         if ($this->getVersion() >= '0.9.0') {
  35.             $this->dbname = $this->mongo->selectDB($dbname);
  36.             $this->db = $this->dbname->selectCollection($table);
  37.         } else {
  38.             $this->db = $this->mongo->$dbname->$table;
  39.         }
  40.     }

  41.     public function getVersion()
  42.     {
  43.         return MongoClient::VERSION;
  44.     }

  45.     /**
  46.      * 單例模式
  47.      * @return Mongo|null
  48.      */
  49.     //public static function getInstance($host=null, $port=null, $dbname=null, $table=null){
  50.     //
  51.     //    if(!(self::$instanceof instanceof self)){
  52.     //        self::$instanceof = new self($host, $port, $dbname, $table);
  53.     //    }
  54.     //
  55.     //    return self::$instanceof;
  56.     //}

  57.     /**
  58.      * 插入一條數(shù)據(jù)
  59.      * @param array $doc
  60.      */
  61.     public function insert($doc = array())
  62.     {
  63.         if (empty($doc)) {
  64.             $this->throwError('插入的數(shù)據(jù)不能為空!');
  65.         }
  66.         //保存數(shù)據(jù)信息
  67.         try {
  68.             if (!$this->db->insert($doc)) {
  69.                 throw new MongoException('插入數(shù)據(jù)失敗');
  70.             }
  71.         } catch (MongoException $e) {
  72.             $this->throwError($e->getMessage());
  73.         }
  74.     }

  75.     /**
  76.      * 插入多條數(shù)據(jù)信息
  77.      * @param array $doc
  78.      */
  79.     public function insertMulti($doc = array())
  80.     {
  81.         if (empty($doc)) {
  82.             $this->throwError('插入的數(shù)據(jù)不能為空!');
  83.         }
  84.         //插入數(shù)據(jù)信息
  85.         foreach ($doc as $key => $val) {
  86.             //判斷$val是不是數(shù)組
  87.             if (is_array($val)) {
  88.                 $this->insert($val);
  89.             }
  90.         }
  91.     }

  92.     /**
  93.      * 查找一條記錄
  94.      * @return array|null
  95.      */
  96.     public function findOne($where = NULL)
  97.     {
  98.         if (NULL === $where) {
  99.             try {
  100.                 if ($result = $this->db->findOne()) {
  101.                     return $result;
  102.                 } else {
  103.                     throw new MongoException('查找數(shù)據(jù)失敗');
  104.                 }
  105.             } catch (MongoException $e) {
  106.                 $this->throwError($e->getMessage());
  107.             }
  108.         } else {
  109.             try {
  110.                 if ($result = $this->db->findOne($where)) {
  111.                     return $result;
  112.                 } else {
  113.                     throw new MongoException('查找數(shù)據(jù)失敗');
  114.                 }
  115.             } catch (MongoException $e) {
  116.                 $this->throwError($e->getMessage());
  117.             }
  118.         }

  119.     }

  120.     /**
  121.      * todo 帶條件的隨后做
  122.      * 查找所有的文檔
  123.      * @return MongoCursor
  124.      */
  125.     public function find($where = NULL)
  126.     {
  127.         if (NULL === $where) {

  128.             try {
  129.                 if ($result = $this->db->find()) {

  130.                 } else {
  131.                     throw new MongoException('查找數(shù)據(jù)失敗');
  132.                 }
  133.             } catch (MongoException $e) {
  134.                 $this->throwError($e->getMessage());
  135.             }
  136.         } else {
  137.             try {
  138.                 if ($result = $this->db->find($where)) {

  139.                 } else {
  140.                     throw new MongoException('查找數(shù)據(jù)失敗');
  141.                 }
  142.             } catch (MongoException $e) {
  143.                 $this->throwError($e->getMessage());
  144.             }
  145.         }

  146.         $arr = array();
  147.         foreach ($result as $id => $val) {
  148.             $arr[] = $val;
  149.         }

  150.         return $arr;
  151.     }

  152.     /**
  153.      * 獲取記錄條數(shù)
  154.      * @return int
  155.      */
  156.     public function getCount()
  157.     {
  158.         try {
  159.             if ($count = $this->db->count()) {
  160.                 return $count;
  161.             } else {
  162.                 throw new MongoException('查找總數(shù)失敗');
  163.             }
  164.         } catch (MongoException $e) {
  165.             $this->throwError($e->getMessage());
  166.         }
  167.     }

  168.     /**
  169.      * 獲取所有的數(shù)據(jù)庫(kù)
  170.      * @return array
  171.      */
  172.     public function getDbs()
  173.     {
  174.         return $this->mongo->listDBs();
  175.     }

  176.     /**
  177.      * 刪除數(shù)據(jù)庫(kù)
  178.      * @param null $dbname
  179.      * @return mixed
  180.      */
  181.     public function dropDb($dbname = NULL)
  182.     {
  183.         if (NULL !== $dbname) {
  184.             $retult = $this->mongo->dropDB($dbname);
  185.             if ($retult['ok']) {
  186.                 return TRUE;
  187.             } else {
  188.                 return FALSE;
  189.             }
  190.         }
  191.         $this->throwError('請(qǐng)輸入要?jiǎng)h除的數(shù)據(jù)庫(kù)名稱');
  192.     }

  193.     /**
  194.      * 強(qiáng)制關(guān)閉數(shù)據(jù)庫(kù)的鏈接
  195.      */
  196.     public function closeDb()
  197.     {
  198.         $this->mongo->close(TRUE);
  199.     }

  200.     /**
  201.      * 輸出錯(cuò)誤信息
  202.      * @param $errorInfo 錯(cuò)誤內(nèi)容
  203.      */
  204.     public function throwError($errorInfo='')
  205.     {
  206.         echo "<h3>出錯(cuò)了:$errorInfo</h3>";
  207.         die();
  208.     }

  209. }
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP