php實(shí)現(xiàn)的mongodb操作類(lèi)
更新時(shí)間:2015年05月28日 11:39:15 投稿:hebedich
說(shuō)到php連mongoDB,不得不先介紹一下php的官方手冊(cè),網(wǎng)址在:http://us.php.net/manual/en/book.mongo.php,接下來(lái)給大家分享一個(gè)本人常用的MONGODB的操作類(lèi),詳見(jiàn)的數(shù)據(jù)庫(kù)操作都有了,小伙伴可以參考下。
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;
/**
* 初始化類(lèi),得到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ù)庫(kù)
* @return array
*/
public function getDbs()
{
return $this->mongo->listDBs();
}
/**
* 刪除數(shù)據(jù)庫(kù)
* @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ù)庫(kù)名稱(chēng)');
}
/**
* 強(qiáng)制關(guān)閉數(shù)據(jù)庫(kù)的鏈接
*/
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)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- php封裝的mongodb操作類(lèi)代碼
- PHP實(shí)現(xiàn)的MongoDB數(shù)據(jù)庫(kù)操作類(lèi)分享
- PHP實(shí)現(xiàn)的mongoDB數(shù)據(jù)庫(kù)操作類(lèi)完整實(shí)例
- php實(shí)現(xiàn)的mongodb操作類(lèi)實(shí)例
- php實(shí)現(xiàn)的mongoDB單例模式操作類(lèi)
- php mongodb操作類(lèi) 帶幾個(gè)簡(jiǎn)單的例子
- PHP mongodb操作類(lèi)定義與用法示例【適合mongodb2.x和mongodb3.x】
- MongoDB操作類(lèi)封裝實(shí)例代碼
相關(guān)文章
微信公眾號(hào)OAuth2.0網(wǎng)頁(yè)授權(quán)問(wèn)題淺析
根據(jù)需求,我今天完成的是微信的網(wǎng)頁(yè)授權(quán)然后拉取用戶(hù)的一些基本信息的問(wèn)題。具體內(nèi)容詳情大家通過(guò)本文學(xué)習(xí)吧2017-01-01
Swoole?webSocket消息服務(wù)系統(tǒng)代碼設(shè)計(jì)詳解
這篇文章主要為大家介紹了Swoole?webSocket消息服務(wù)系統(tǒng)代碼設(shè)計(jì)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
laravel 模型查詢(xún)按照whereIn排序的示例
今天小編就為大家分享一篇laravel 模型查詢(xún)按照whereIn排序的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
WordPress特定文章對(duì)搜索引擎隱藏或只允許搜索引擎查看
這篇文章主要介紹了WordPress特定文章對(duì)搜索引擎隱藏或只允許搜索引擎查看的方法,可以根據(jù)SEO的需要來(lái)進(jìn)行調(diào)整,需要的朋友可以參考下2015-12-12
PHP實(shí)現(xiàn)的微信APP支付功能示例【基于TP5框架】
這篇文章主要介紹了PHP實(shí)現(xiàn)的微信APP支付功能,結(jié)合實(shí)例形式分析了基于TP5框架的微信APP支付相關(guān)原理、操作步驟與處理技巧,需要的朋友可以參考下2019-09-09

