php實(shí)現(xiàn)的mongodb操作類
更新時(shí)間:2015年05月28日 11:39:15 投稿:hebedich
說到php連mongoDB,不得不先介紹一下php的官方手冊(cè),網(wǎng)址在:http://us.php.net/manual/en/book.mongo.php,接下來給大家分享一個(gè)本人常用的MONGODB的操作類,詳見的數(shù)據(jù)庫操作都有了,小伙伴可以參考下。
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的實(shí)例對(duì)象 */ 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; //} /** * 插入一條數(shù)據(jù) * @param array $doc */ public function insert($doc = array()) { if (empty($doc)) { $this->throwError('插入的數(shù)據(jù)不能為空!'); } //保存數(shù)據(jù)信息 try { if (!$this->db->insert($doc)) { throw new MongoException('插入數(shù)據(jù)失敗'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } /** * 插入多條數(shù)據(jù)信息 * @param array $doc */ public function insertMulti($doc = array()) { if (empty($doc)) { $this->throwError('插入的數(shù)據(jù)不能為空!'); } //插入數(shù)據(jù)信息 foreach ($doc as $key => $val) { //判斷$val是不是數(shù)組 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('查找數(shù)據(jù)失敗'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } else { try { if ($result = $this->db->findOne($where)) { return $result; } else { throw new MongoException('查找數(shù)據(jù)失敗'); } } 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('查找數(shù)據(jù)失敗'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } else { try { if ($result = $this->db->find($where)) { } else { throw new MongoException('查找數(shù)據(jù)失敗'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } $arr = array(); foreach ($result as $id => $val) { $arr[] = $val; } return $arr; } /** * 獲取記錄條數(shù) * @return int */ public function getCount() { try { if ($count = $this->db->count()) { return $count; } else { throw new MongoException('查找總數(shù)失敗'); } } catch (MongoException $e) { $this->throwError($e->getMessage()); } } /** * 獲取所有的數(shù)據(jù)庫 * @return array */ public function getDbs() { return $this->mongo->listDBs(); } /** * 刪除數(shù)據(jù)庫 * @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('請(qǐng)輸入要?jiǎng)h除的數(shù)據(jù)庫名稱'); } /** * 強(qiáng)制關(guān)閉數(shù)據(jù)庫的鏈接 */ public function closeDb() { $this->mongo->close(TRUE); } /** * 輸出錯(cuò)誤信息 * @param $errorInfo 錯(cuò)誤內(nèi)容 */ public function throwError($errorInfo='') { echo "<h3>出錯(cuò)了:$errorInfo</h3>"; die(); } }
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
微信公眾號(hào)OAuth2.0網(wǎng)頁授權(quán)問題淺析
根據(jù)需求,我今天完成的是微信的網(wǎng)頁授權(quán)然后拉取用戶的一些基本信息的問題。具體內(nèi)容詳情大家通過本文學(xué)習(xí)吧2017-01-01Swoole?webSocket消息服務(wù)系統(tǒng)代碼設(shè)計(jì)詳解
這篇文章主要為大家介紹了Swoole?webSocket消息服務(wù)系統(tǒng)代碼設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03WordPress特定文章對(duì)搜索引擎隱藏或只允許搜索引擎查看
這篇文章主要介紹了WordPress特定文章對(duì)搜索引擎隱藏或只允許搜索引擎查看的方法,可以根據(jù)SEO的需要來進(jìn)行調(diào)整,需要的朋友可以參考下2015-12-12PHP實(shí)現(xiàn)的微信APP支付功能示例【基于TP5框架】
這篇文章主要介紹了PHP實(shí)現(xiàn)的微信APP支付功能,結(jié)合實(shí)例形式分析了基于TP5框架的微信APP支付相關(guān)原理、操作步驟與處理技巧,需要的朋友可以參考下2019-09-09