a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁(yè) > 數(shù)據(jù)庫(kù) > MongoDB > 正文

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

2020-03-22 16:29:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了PHP實(shí)現(xiàn)的mongoDB數(shù)據(jù)庫(kù)操作類,結(jié)合完整實(shí)例形式詳細(xì)分析了php基于單例模式針對(duì)mongoDB數(shù)據(jù)庫(kù)連接、增刪改查、統(tǒng)計(jì)等操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

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

最近的項(xiàng)目開(kāi)發(fā)中使用的數(shù)據(jù)庫(kù)是mongodb數(shù)據(jù)庫(kù),因?yàn)樾【幍墓疽彩莿倓偸褂胢ongodb數(shù)據(jù)庫(kù),所以之前沒(méi)有封裝好的mongodb數(shù)據(jù)庫(kù)操作類拿來(lái)使用,所以小編在項(xiàng)目中自己封裝了一個(gè)mongodb數(shù)據(jù)庫(kù)操作類,特拿出來(lái)分享,不盡人意的地方希望大家勿噴。

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

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

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

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

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

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

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

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

在32位模式運(yùn)行時(shí)支持的最大文件尺寸為2GB。

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

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

 ?php * PHP操作mongodb數(shù)據(jù)庫(kù)操作類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ù)庫(kù)單例方法 * @param $server * @param $user * @param $password * @param $port * @return Mongo public function getInstance($server, $user, $password, $port) { if (isset($this- mo)) { return $this-  } 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-  * 查詢表中所有數(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)計(jì)個(gè)數(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)計(jì)個(gè)數(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 ? 

您可能感興趣的文章:

ThinkPHP框架使用redirect實(shí)現(xiàn)頁(yè)面重定向的方法實(shí)例講解

php 字符串中是否包含指定字符串的多種方法講解

php框架CodeIgniter使用redis的方法講解

以上就是PHP實(shí)現(xiàn)的mongoDB數(shù)據(jù)庫(kù)操作類完整實(shí)例講解的詳細(xì)內(nèi)容,PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美日本一区 | 午夜一级黄色片 | 欧美猛交ⅹxxx乱大交视频 | 国产一区二区三区网站 | 最新国产精品 | 欧美视频网站 | 日韩中文字幕在线观看 | 四虎国产成人永久精品免费 | 一级毛片在线 | 国产欧美精品 | 欧美一区二区三区在线视频观看 | 日本一区二区不卡 | 精品无人乱码一区二区三区 | 欧美日韩精品一区二区三区在线观看 | 久久久激情 | 国产精品日韩 | 中文av网站 | 欧美激情一区二区三区蜜桃视频 | 欧美午夜激情在线 | 日韩欧美中文在线观看 | 亚洲精品一区中文字幕乱码 | 久久久久亚洲国产 | 国产视频二区 | 天天爱天天操 | 亚洲欧美一区二区三区在线 | 九九热视频在线 | 精品九九九 | 干狠狠| 亚洲三级视频 | 日韩中文字幕在线 | 日韩中文字幕在线视频 | 日韩国产欧美 | 吊视频一区二区三区 | av一区二区在线播放 | av日韩在线看 | 久久精品免费观看 | 亚洲国产一区二区在线 | 久久精品国产亚洲 | 男人操女人bb视频 | 国产成人高清精品免费5388 | 亚洲精品久久久久久久久久久久久 |