欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)的mongodb操作類

 更新時(shí)間:2015年05月28日 11:39:15   投稿:hebedich  
說到php連mongoDB,不得不先介紹一下php的官方手冊(cè),網(wǎng)址在:http://us.php.net/manual/en/book.mongo.php,接下來給大家分享一個(gè)本人常用的MONGODB的操作類,詳見的數(shù)據(jù)庫操作都有了,小伙伴可以參考下。

mongo_db.php

<?php
 
/**
 * Created by PhpStorm.
 * User: yangyulong
 * Date: 2015/5/26
 * Time: 13:45
 */
class Mongo_db
{
  private static $instanceof = NULL;
  public $mongo;
  private $host = 'localhost';
  private $port = '27017';
 
  private $db;
  public $dbname;
  private $table = NULL;
 
  /**
   * 初始化類,得到mongo的實(shí)例對(duì)象
   */
  public function __construct($host = NULL, $port = NULL, $dbname = NULL, $table = NULL)
  {
 
    if (NULL === $dbname) {
      $this->throwError('集合不能為空!');
    }
 
    //判斷是否傳遞了host和port
    if (NULL !== $host) {
      $this->host = $host;
    }
 
    if (NULL !== $port) {
      $this->port = $port;
    }
 
    $this->table = $table;
 
    $this->mongo = new MongoClient($this->host . ':' . $this->port);
    if ($this->getVersion() >= '0.9.0') {
      $this->dbname = $this->mongo->selectDB($dbname);
      $this->db = $this->dbname->selectCollection($table);
    } else {
      $this->db = $this->mongo->$dbname->$table;
    }
  }
 
  public function getVersion()
  {
    return MongoClient::VERSION;
  }
 
  /**
   * 單例模式
   * @return Mongo|null
   */
  //public static function getInstance($host=null, $port=null, $dbname=null, $table=null){
  //
  //  if(!(self::$instanceof instanceof self)){
  //    self::$instanceof = new self($host, $port, $dbname, $table);
  //  }
  //
  //  return self::$instanceof;
  //}
 
  /**
   * 插入一條數(shù)據(jù)
   * @param array $doc
   */
  public function insert($doc = array())
  {
    if (empty($doc)) {
      $this->throwError('插入的數(shù)據(jù)不能為空!');
    }
    //保存數(shù)據(jù)信息
    try {
      if (!$this->db->insert($doc)) {
        throw new MongoException('插入數(shù)據(jù)失敗');
      }
    } catch (MongoException $e) {
      $this->throwError($e->getMessage());
    }
  }
 
  /**
   * 插入多條數(shù)據(jù)信息
   * @param array $doc
   */
  public function insertMulti($doc = array())
  {
    if (empty($doc)) {
      $this->throwError('插入的數(shù)據(jù)不能為空!');
    }
    //插入數(shù)據(jù)信息
    foreach ($doc as $key => $val) {
      //判斷$val是不是數(shù)組
      if (is_array($val)) {
        $this->insert($val);
      }
    }
  }
 
  /**
   * 查找一條記錄
   * @return array|null
   */
  public function findOne($where = NULL)
  {
    if (NULL === $where) {
      try {
        if ($result = $this->db->findOne()) {
          return $result;
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    } else {
      try {
        if ($result = $this->db->findOne($where)) {
          return $result;
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    }
 
  }
 
  /**
   * todo 帶條件的隨后做
   * 查找所有的文檔
   * @return MongoCursor
   */
  public function find($where = NULL)
  {
    if (NULL === $where) {
 
      try {
        if ($result = $this->db->find()) {
 
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    } else {
      try {
        if ($result = $this->db->find($where)) {
 
        } else {
          throw new MongoException('查找數(shù)據(jù)失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    }
 
    $arr = array();
    foreach ($result as $id => $val) {
      $arr[] = $val;
    }
 
    return $arr;
  }
 
  /**
   * 獲取記錄條數(shù)
   * @return int
   */
  public function getCount()
  {
    try {
      if ($count = $this->db->count()) {
        return $count;
      } else {
        throw new MongoException('查找總數(shù)失敗');
      }
    } catch (MongoException $e) {
      $this->throwError($e->getMessage());
    }
  }
 
  /**
   * 獲取所有的數(shù)據(jù)庫
   * @return array
   */
  public function getDbs()
  {
    return $this->mongo->listDBs();
  }
 
  /**
   * 刪除數(shù)據(jù)庫
   * @param null $dbname
   * @return mixed
   */
  public function dropDb($dbname = NULL)
  {
    if (NULL !== $dbname) {
      $retult = $this->mongo->dropDB($dbname);
      if ($retult['ok']) {
        return TRUE;
      } else {
        return FALSE;
      }
    }
    $this->throwError('請(qǐng)輸入要?jiǎng)h除的數(shù)據(jù)庫名稱');
  }
 
  /**
   * 強(qiáng)制關(guān)閉數(shù)據(jù)庫的鏈接
   */
  public function closeDb()
  {
    $this->mongo->close(TRUE);
  }
 
  /**
   * 輸出錯(cuò)誤信息
   * @param $errorInfo 錯(cuò)誤內(nèi)容
   */
  public function throwError($errorInfo='')
  {
    echo "<h3>出錯(cuò)了:$errorInfo</h3>";
    die();
  }
 
}

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評(píng)論