php實現(xiàn)的mongodb操作類
更新時間:2015年05月28日 11:39:15 投稿:hebedich
說到php連mongoDB,不得不先介紹一下php的官方手冊,網(wǎng)址在:http://us.php.net/manual/en/book.mongo.php,接下來給大家分享一個本人常用的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的實例對象
*/
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('請輸入要刪除的數(shù)據(jù)庫名稱');
}
/**
* 強制關閉數(shù)據(jù)庫的鏈接
*/
public function closeDb()
{
$this->mongo->close(TRUE);
}
/**
* 輸出錯誤信息
* @param $errorInfo 錯誤內(nèi)容
*/
public function throwError($errorInfo='')
{
echo "<h3>出錯了:$errorInfo</h3>";
die();
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關文章
Swoole?webSocket消息服務系統(tǒng)代碼設計詳解
這篇文章主要為大家介紹了Swoole?webSocket消息服務系統(tǒng)代碼設計詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
WordPress特定文章對搜索引擎隱藏或只允許搜索引擎查看
這篇文章主要介紹了WordPress特定文章對搜索引擎隱藏或只允許搜索引擎查看的方法,可以根據(jù)SEO的需要來進行調(diào)整,需要的朋友可以參考下2015-12-12
PHP實現(xiàn)的微信APP支付功能示例【基于TP5框架】
這篇文章主要介紹了PHP實現(xiàn)的微信APP支付功能,結(jié)合實例形式分析了基于TP5框架的微信APP支付相關原理、操作步驟與處理技巧,需要的朋友可以參考下2019-09-09

