PHP+mongoDB資料庫操作步驟詳解

php中世界最好的语言
發布: 2023-03-26 07:08:02
原創
1896 人瀏覽過

這次帶給大家PHP mongoDB資料庫操作步驟詳解,PHP mongoDB資料庫的注意事項有哪些,下面就是實戰案例,一起來看一下。

最近的專案開發中使用的資料庫是mongodb資料庫,因為小編的公司也是剛使用mongodb資料庫,所以之前沒有封裝好的mongodb資料庫操作類別拿來使用,所以小編在專案中自己封裝了一個mongodb資料庫操作類,特拿出來分享,不盡人意的地方希望大家勿噴。

眾所周知,mongodb是典型的nosql資料庫的代表,受到很多開發者的追捧,近年來尤為火熱,mongodb的流行不是沒有原因的,下邊給大家簡單介紹下MongoDB。

MongoDB是一個介於關聯式資料庫和非關聯式資料庫之間的產品,是非關聯式資料庫當中功能最豐富,最像關聯式資料庫的。他支援的資料結構非常鬆散,是類似json的bjson格式,因此可以儲存比較複雜的資料類型。 Mongo最大的特點是他支援的查詢語言非常強大,其語法有點類似於物件導向的查詢語言,幾乎可以實現類似關聯式資料庫單表查詢的絕大部分功能,而且還支援對數據建立索引。

它的特點是高效能、易於部署、易於使用,儲存資料非常方便。主要功能特性有:

面向集合存儲,易儲存物件類型的資料。
模式自由。
支援動態查詢。
支援完全索引,包含內部物件。
支援查詢。
支援複製和故障復原。
使用高效的二進位資料存儲,包括大型物件(如視訊等)。
自動處理碎片,以支援雲端運算層次的擴展性
支援RUBY,PYTHON,JAVA,C ,PHP等多種語言。
檔案儲存格式為BSON(一種JSON的擴充)
可透過網路存取

#所謂「面向集合」(Collenction-Orented),意思是資料被分組儲存在數據集中,被稱為一個集合(Collenction)。每個 集合在資料庫中都有一個唯一的識別名,並且可以包含無限數目的文件。集合的概念類似關係型資料庫(RDBMS)裡的表格(table),不同的是它不需要定 義任何模式(schema)。

模式自由(schema-free),意味著對於儲存在mongodb資料庫中的文件,我們不需要知道它的任何結構定義。如果需要的話,你完全可以把不同結構的檔案儲存在同一個資料庫裡。

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

MongoDB服務端可運作在Linux、Windows或OS X平台,支援32位元和64位元應用,預設連接埠為27017。建議運行在64位元平台,因為MongoDB

在32位元模式運行時支援的最大檔案尺寸為2GB。

MongoDB把資料儲存在檔案中(預設路徑為:/data/db),為提高效率使用記憶體映射檔進行管理。

<?php
/**
 * PHP操作mongodb数据库操作类
 */
class Database {
  protected $database  = &#39;&#39;;
  protected $mo;
  /**
   * 构造方法
   */
  public function construct() {
    $server = DBSERVER;
    $user = DBUSER;
    $password = DBPASS;
    $port = DBPORT;
    $database = DBNAME;
    $mongo = $this->getInstance($server, $user, $password, $port);
    $this->database = $mongo->$database;
  }
  /**
   * 数据库单例方法
   * @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;
    }
  }
  /**
   * 查询表中所有数据
   * @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 = &#39;&#39;, $skip = &#39;&#39;) {
    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;
  }
  /**
   * 查询指定一条数据
   * @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;
  }
  /**
   * 统计个数
   * @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;
  }
  /**
   * 直接执行mongo命令
   * @param $sql
   * @return array
   */
  public function toExcute($sql) {
    $result = $this->database->execute($sql);
    return $result;
  }
  /**
   * 分组统计个数
   * @param $table
   * @param $where
   * @param $field
   */
  public function groupCount($table, $where, $field) {
    $cond = array(
      array(
        &#39;$match&#39; => $where,
      ),
      array(
        &#39;$group&#39; => array(
          &#39;_id&#39; => &#39;$&#39; . $field,
          &#39;count&#39; => array(&#39;$sum&#39; => 1),
        ),
      ),
      array(
        &#39;$sort&#39; => array("count" => -1),
      ),
    );
    $this->database->$table->aggregate($cond);
  }
  /**
   * 删除数据
   * @param $table
   * @param $where
   * @return array|bool
   */
  public function toDelete($table, $where) {
    $re = $this->database->$table->remove($where);
    return $re;
  }
  /**
   * 插入数据
   * @param $table
   * @param $data
   * @return array|bool
   */
  public function toInsert($table, $data) {
    $re = $this->database->$table->insert($data);
    return $re;
  }
  /**
   * 更新数据
   * @param $table
   * @param $where
   * @param $data
   * @return bool
   */
  public function toUpdate($table, $where, $data) {
    $re = $this->database->$table->update($where, array(&#39;$set&#39; => $data));
    return $re;
  }
  /**
   * 获取唯一数据
   * @param $table
   * @param $key
   * @return array
   */
  public function distinctData($table, $key, $query = array()) {
    if (!empty($query)) {
      $where = array(&#39;distinct&#39; => $table, &#39;key&#39; => $key, &#39;query&#39; => $query);
    } else {
      $where = array(&#39;distinct&#39; => $table, &#39;key&#39; => $key);
    }
    $data = $this->database->command($where);
    return $data[&#39;values&#39;];
  }
}
?>
登入後複製

我相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

php微信公眾號隨機發放現金紅包功能

PHP實作負載平衡下的session共用案列詳解(附代碼)

以上是PHP+mongoDB資料庫操作步驟詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!