這篇文章主要介紹了PHP實現的mongoDB資料庫操作類別,結合完整實例形式詳細分析了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作業MongoDB資料庫的資料庫操作類別原始碼如下,僅供參考。
<?php /** * PHP操作mongodb数据库操作类 */ class Database { protected $database = ''; 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 = '', $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; } /** * 查询指定一条数据 * @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( '$match' => $where, ), array( '$group' => array( '_id' => '$' . $field, 'count' => array('$sum' => 1), ), ), array( '$sort' => 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('$set' => $data)); return $re; } /** * 获取唯一数据 * @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實作頁面重定向的方法實例講解
以上是PHP實作的mongoDB資料庫操作類別完整實例講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

你應該關心DependencyInjection(DI),因為它能讓你的代碼更清晰、更易維護。 1)DI通過解耦類,使其更模塊化,2)提高了測試的便捷性和代碼的靈活性,3)使用DI容器可以管理複雜的依賴關係,但要注意性能影響和循環依賴問題,4)最佳實踐是依賴於抽象接口,實現鬆散耦合。

是的,優化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)優化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,並避免使用

theKeyStrategiestosigantificallyBoostPhpaPplicationPerformenCeare:1)UseOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)優化AtabaseInteractionswithPreparedStateTementStatementStatementAndProperIndexing,3)配置

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增強codemodocultion,可驗證性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

選擇DependencyInjection(DI)用於大型應用,ServiceLocator適合小型項目或原型。 1)DI通過構造函數注入依賴,提高代碼的測試性和模塊化。 2)ServiceLocator通過中心註冊獲取服務,方便但可能導致代碼耦合度增加。

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)啟用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替換loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

phpemailvalidation invoLvesthreesteps:1)格式化進行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!