成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? php實現的mongodb操作類
全站頭部文字 我要出現在這里
php實現的mongodb操作類 769 2023-12-10   

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的實例對象
   */
  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;
  //}
 
  /**
   * 插入一條數據
   * @param array $doc
   */
  public function insert($doc = array())
  {
    if (empty($doc)) {
      $this->throwError('插入的數據不能為空!');
    }
    //保存數據信息
    try {
      if (!$this->db->insert($doc)) {
        throw new MongoException('插入數據失敗');
      }
    } catch (MongoException $e) {
      $this->throwError($e->getMessage());
    }
  }
 
  /**
   * 插入多條數據信息
   * @param array $doc
   */
  public function insertMulti($doc = array())
  {
    if (empty($doc)) {
      $this->throwError('插入的數據不能為空!');
    }
    //插入數據信息
    foreach ($doc as $key => $val) {
      //判斷$val是不是數組
      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('查找數據失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    } else {
      try {
        if ($result = $this->db->findOne($where)) {
          return $result;
        } else {
          throw new MongoException('查找數據失敗');
        }
      } 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('查找數據失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    } else {
      try {
        if ($result = $this->db->find($where)) {
 
        } else {
          throw new MongoException('查找數據失敗');
        }
      } catch (MongoException $e) {
        $this->throwError($e->getMessage());
      }
    }
 
    $arr = array();
    foreach ($result as $id => $val) {
      $arr[] = $val;
    }
 
    return $arr;
  }
 
  /**
   * 獲取記錄條數
   * @return int
   */
  public function getCount()
  {
    try {
      if ($count = $this->db->count()) {
        return $count;
      } else {
        throw new MongoException('查找總數失敗');
      }
    } catch (MongoException $e) {
      $this->throwError($e->getMessage());
    }
  }
 
  /**
   * 獲取所有的數據庫
   * @return array
   */
  public function getDbs()
  {
    return $this->mongo->listDBs();
  }
 
  /**
   * 刪除數據庫
   * @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('請輸入要刪除的數據庫名稱');
  }
 
  /**
   * 強制關閉數據庫的鏈接
   */
  public function closeDb()
  {
    $this->mongo->close(TRUE);
  }
 
  /**
   * 輸出錯誤信息
   * @param $errorInfo 錯誤內容
   */
  public function throwError($errorInfo='')
  {
    echo "<h3>出錯了:$errorInfo</h3>";
    die();
  }
 
}

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



主站蜘蛛池模板: 圣斗士星矢在线观看| 白上之黑电影| chinese国产xxx实拍| 迷失安狄| 电视剧零下三十八度演员表| a面b面| 明日战记| 四大名著好词好句摘抄| 绝伦海女| 中央五节目表| 电子版租房合同免费下载| 密桃成孰时| 古装发型| 罪恋电影| 诗经中使用叠词的诗句| 汤唯和梁朝伟拍戏原版视频在线观看| free gay movies| 凌靖| 爱情最美丽 电视剧| 坏孩子电影| 就义诗诗配画| 情欲迷宫电影| 预备党员季度思想汇报| 挠vk| av电影网| 大悲咒朗诵正版念诵| 糟老头视频下载| 难兄难弟 电视剧| 张志忠演员| 谁的青春不迷茫 电影| 骨骺线闭合增高9厘米| 汤图片大全高清图片| soldier's heart| 菊花开| 秀人网陆萱萱| 《杨贵妃淫史》三级| 欧美日本视频在线| 凯西·贝茨| 新上海滩张国荣和宁静| 美女亲热| 调音师结局剧情大解析|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103