主頁 > 知識庫 > PHP實現(xiàn)的mongoDB數(shù)據(jù)庫操作類完整實例

PHP實現(xiàn)的mongoDB數(shù)據(jù)庫操作類完整實例

熱門標簽:電話機器人銷售主要負責(zé)什么 寧波外呼營銷系統(tǒng) 房產(chǎn)中介用的是什么外呼系統(tǒng) 福建銀行智能外呼系統(tǒng)價格 遼寧ai電銷機器人價格 地圖標注專員怎么樣 四川保險智能外呼系統(tǒng)供應(yīng)商 長沙做地圖標注公司 上海做外呼線路的通信公司

本文實例講述了PHP實現(xiàn)的mongoDB數(shù)據(jù)庫操作類。分享給大家供大家參考,具體如下:

最近的項目開發(fā)中使用的數(shù)據(jù)庫是mongodb數(shù)據(jù)庫,因為小編的公司也是剛剛使用mongodb數(shù)據(jù)庫,所以之前沒有封裝好的mongodb數(shù)據(jù)庫操作類拿來使用,所以小編在項目中自己封裝了一個mongodb數(shù)據(jù)庫操作類,特拿出來分享,不盡人意的地方希望大家勿噴。

眾所周知,mongodb是典型的nosql數(shù)據(jù)庫的代表,受到很多開發(fā)者的追捧,近幾年尤為火熱,mongodb的流行不是沒有原因的,下邊給大家簡單介紹下MongoDB。

MongoDB是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。他支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是類似json的bjson格式,因此可以存儲比較復(fù)雜的數(shù)據(jù)類型。Mongo最大的特點是他支持的查詢語言非常強大,其語法有點類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。

它的特點是高性能、易部署、易使用,存儲數(shù)據(jù)非常方便。主要功能特性有:

面向集合存儲,易存儲對象類型的數(shù)據(jù)。
模式自由。
支持動態(tài)查詢。
支持完全索引,包含內(nèi)部對象。
支持查詢。
支持復(fù)制和故障恢復(fù)。
使用高效的二進制數(shù)據(jù)存儲,包括大型對象(如視頻等)。
自動處理碎片,以支持云計算層次的擴展性
支持RUBY,PYTHON,JAVA,C++,PHP等多種語言。
文件存儲格式為BSON(一種JSON的擴展)
可通過網(wǎng)絡(luò)訪問

所謂“面向集合”(Collenction-Orented),意思是數(shù)據(jù)被分組存儲在數(shù)據(jù)集中,被稱為一個集合(Collenction)。每個 集合在數(shù)據(jù)庫中都有一個唯一的標識名,并且可以包含無限數(shù)目的文檔。集合的概念類似關(guān)系型數(shù)據(jù)庫(RDBMS)里的表(table),不同的是它不需要定 義任何模式(schema)。

模式自由(schema-free),意味著對于存儲在mongodb數(shù)據(jù)庫中的文件,我們不需要知道它的任何結(jié)構(gòu)定義。如果需要的話,你完全可以把不同結(jié)構(gòu)的文件存儲在同一個數(shù)據(jù)庫里。

存儲在集合中的文檔,被存儲為鍵-值對的形式。鍵用于唯一標識一個文檔,為字符串類型,而值則可以是各中復(fù)雜的文件類型。我們稱這種存儲形式為BSON(Binary Serialized dOcument Format)。

MongoDB服務(wù)端可運行在Linux、Windows或OS X平臺,支持32位和64位應(yīng)用,默認端口為27017。推薦運行在64位平臺,因為MongoDB

在32位模式運行時支持的最大文件尺寸為2GB。

MongoDB把數(shù)據(jù)存儲在文件中(默認路徑為:/data/db),為提高效率使用內(nèi)存映射文件進行管理。

小編自己封裝的PHP操作MongoDB數(shù)據(jù)庫的數(shù)據(jù)庫操作類源碼如下,僅供參考。

?php
/**
 * PHP操作mongodb數(shù)據(jù)庫操作類
 */
class Database {
  protected $database  = '';
  protected $mo;
  /**
   * 構(gòu)造方法
   */
  public function __construct() {
    $server = DBSERVER;
    $user = DBUSER;
    $password = DBPASS;
    $port = DBPORT;
    $database = DBNAME;
    $mongo = $this->getInstance($server, $user, $password, $port);
    $this->database = $mongo->$database;
  }
  /**
   * 數(shù)據(jù)庫單例方法
   * @param $server
   * @param $user
   * @param $password
   * @param $port
   * @return Mongo
   */
  public function getInstance($server, $user, $password, $port) {
    if (isset($this->mo)) {
      return $this->mo;
    } else {
      if (!empty($server)) {
        if (!empty($port)) {
          if (!empty($user)  !empty($password)) {
            $this->mo = new Mongo("mongodb://{$user}:{$password}@{$server}:{$port}");
          } else {
            $this->mo = new Mongo("mongodb://{$server}:{$port}");
          }
        } else {
          $this->mo = new Mongo("mongodb://{$server}");
        }
      } else {
        $this->mo = new Mongo();
      }
      return $this->mo;
    }
  }
  /**
   * 查詢表中所有數(shù)據(jù)
   * @param $table
   * @param array $where
   * @param array $sort
   * @param string $limit
   * @param string $skip
   * @return array|int
   */
  public function getAll($table, $where = array(), $sort = array(), $limit = '', $skip = '') {
    if (!empty($where)) {
      $data = $this->database->$table->find($where);
    } else {
      $data = $this->database->$table->find();
    }
    if (!empty($sort)) {
      $data = $data->sort($sort);
    }
    if (!empty($limit)) {
      $data = $data->limit($limit);
    }
    if (!empty($skip)) {
      $data = $data->skip($skip);
    }
    $newData = array();
    while ($data->hasNext()) {
      $newData[] = $data->getNext();
    }
    if (count($newData) == 0) {
      return 0;
    }
    return $newData;
  }
  /**
   * 查詢指定一條數(shù)據(jù)
   * @param $table
   * @param array $where
   * @return int
   */
  public function getOne($table, $where = array()) {
    if (!empty($where)) {
      $data = $this->database->$table->findOne($where);
    } else {
      $data = $this->database->$table->findOne();
    }
    return $data;
  }
  /**
   * 統(tǒng)計個數(shù)
   * @param $table
   * @param array $where
   * @return mixed
   */
  public function getCount($table, $where = array()) {
    if (!empty($where)) {
      $data = $this->database->$table->find($where)->count();
    } else {
      $data = $this->database->$table->find()->count();
    }
    return $data;
  }
  /**
   * 直接執(zhí)行mongo命令
   * @param $sql
   * @return array
   */
  public function toExcute($sql) {
    $result = $this->database->execute($sql);
    return $result;
  }
  /**
   * 分組統(tǒng)計個數(shù)
   * @param $table
   * @param $where
   * @param $field
   */
  public function groupCount($table, $where, $field) {
    $cond = array(
      array(
        '$match' => $where,
      ),
      array(
        '$group' => array(
          '_id' => '$' . $field,
          'count' => array('$sum' => 1),
        ),
      ),
      array(
        '$sort' => array("count" => -1),
      ),
    );
    $this->database->$table->aggregate($cond);
  }
  /**
   * 刪除數(shù)據(jù)
   * @param $table
   * @param $where
   * @return array|bool
   */
  public function toDelete($table, $where) {
    $re = $this->database->$table->remove($where);
    return $re;
  }
  /**
   * 插入數(shù)據(jù)
   * @param $table
   * @param $data
   * @return array|bool
   */
  public function toInsert($table, $data) {
    $re = $this->database->$table->insert($data);
    return $re;
  }
  /**
   * 更新數(shù)據(jù)
   * @param $table
   * @param $where
   * @param $data
   * @return bool
   */
  public function toUpdate($table, $where, $data) {
    $re = $this->database->$table->update($where, array('$set' => $data));
    return $re;
  }
  /**
   * 獲取唯一數(shù)據(jù)
   * @param $table
   * @param $key
   * @return array
   */
  public function distinctData($table, $key, $query = array()) {
    if (!empty($query)) {
      $where = array('distinct' => $table, 'key' => $key, 'query' => $query);
    } else {
      $where = array('distinct' => $table, 'key' => $key);
    }
    $data = $this->database->command($where);
    return $data['values'];
  }
}
?>

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MongoDB數(shù)據(jù)庫操作技巧大全》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

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

您可能感興趣的文章:
  • php封裝的mongodb操作類代碼
  • PHP實現(xiàn)的MongoDB數(shù)據(jù)庫操作類分享
  • php實現(xiàn)的mongodb操作類
  • php實現(xiàn)的mongodb操作類實例
  • php實現(xiàn)的mongoDB單例模式操作類
  • php mongodb操作類 帶幾個簡單的例子
  • PHP mongodb操作類定義與用法示例【適合mongodb2.x和mongodb3.x】
  • MongoDB操作類封裝實例代碼

標簽:延安 宜春 澳門 佛山 常德 宿遷 工商登記 深圳

巨人網(wǎng)絡(luò)通訊聲明:本文標題《PHP實現(xiàn)的mongoDB數(shù)據(jù)庫操作類完整實例》,本文關(guān)鍵詞  PHP,實現(xiàn),的,mongoDB,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP實現(xiàn)的mongoDB數(shù)據(jù)庫操作類完整實例》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP實現(xiàn)的mongoDB數(shù)據(jù)庫操作類完整實例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章