php实现的mongodb操作类
程序员文章站
2023-08-12 14:22:31
mongo_db.php
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; //} /** * 插入一条数据 * @param array $doc */ public function insert($doc = array()) { if (empty($doc)) { $this->throwerror('插入的数据不能为空!'); } //保存数据信息 try { if (!$this->db->insert($doc)) { throw new mongoexception('插入数据失败'); } } catch (mongoexception $e) { $this->throwerror($e->getmessage()); } } /** * 插入多条数据信息 * @param array $doc */ public function insertmulti($doc = array()) { if (empty($doc)) { $this->throwerror('插入的数据不能为空!'); } //插入数据信息 foreach ($doc as $key => $val) { //判断$val是不是数组 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('查找数据失败'); } } catch (mongoexception $e) { $this->throwerror($e->getmessage()); } } else { try { if ($result = $this->db->findone($where)) { return $result; } else { throw new mongoexception('查找数据失败'); } } 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('查找数据失败'); } } catch (mongoexception $e) { $this->throwerror($e->getmessage()); } } else { try { if ($result = $this->db->find($where)) { } else { throw new mongoexception('查找数据失败'); } } catch (mongoexception $e) { $this->throwerror($e->getmessage()); } } $arr = array(); foreach ($result as $id => $val) { $arr[] = $val; } return $arr; } /** * 获取记录条数 * @return int */ public function getcount() { try { if ($count = $this->db->count()) { return $count; } else { throw new mongoexception('查找总数失败'); } } catch (mongoexception $e) { $this->throwerror($e->getmessage()); } } /** * 获取所有的数据库 * @return array */ public function getdbs() { return $this->mongo->listdbs(); } /** * 删除数据库 * @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('请输入要删除的数据库名称'); } /** * 强制关闭数据库的链接 */ public function closedb() { $this->mongo->close(true); } /** * 输出错误信息 * @param $errorinfo 错误内容 */ public function throwerror($errorinfo='') { echo "<h3>出错了:$errorinfo</h3>"; die(); } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇: 微鹿客发力车辆运营行业 发挥广告商业价值