php实现的mongodb操作类


Posted in PHP onMay 28, 2015

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();
  }
 
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

PHP 相关文章推荐
PHP 函数执行效率的小比较
Oct 17 PHP
基于Snoopy的PHP近似完美获取网站编码的代码
Oct 23 PHP
php数组函数序列之array_sum() - 计算数组元素值之和
Oct 29 PHP
PHP file_exists问题杂谈
May 07 PHP
php实现快速排序法函数代码
Aug 27 PHP
php模拟ping命令(php exec函数的使用方法)
Oct 25 PHP
PHP中的命名空间相关概念浅析
Jan 22 PHP
Nginx下配置codeigniter框架方法
Apr 07 PHP
PHP使用CURL实现多线程抓取网页
Apr 30 PHP
zend framework中使用memcache的方法
Mar 04 PHP
PHP  Yii清理缓存的实现方法
Nov 10 PHP
Laravel 实现密码重置功能
Feb 23 PHP
PHP编译安装时常见错误解决办法
May 28 #PHP
PHP安装memcached扩展笔记
May 28 #PHP
PHP实现的增强性mhash函数
May 27 #PHP
PHP验证信用卡卡号是否正确函数
May 27 #PHP
PHP的伪随机数与真随机数详解
May 27 #PHP
php实现window平台的checkdnsrr函数
May 27 #PHP
PHP实现恶意DDOS攻击避免带宽占用问题方法
May 27 #PHP
You might like
PHP如何得到当前页和上一页的地址?
2006/11/27 PHP
开启CURL扩展,让服务器支持PHP curl函数(远程采集)
2011/03/19 PHP
PHP的魔术常量__METHOD__简介
2014/07/08 PHP
php使用Jpgraph绘制饼状图的方法
2015/06/10 PHP
discuz图片顺序混乱解决方案
2015/07/29 PHP
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
2016/03/05 PHP
thinkPHP3.1验证码的简单实现方法
2016/04/22 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
ie 调试javascript的工具
2009/04/29 Javascript
Javascript下IE与Firefox下的差异兼容写法总结
2010/06/18 Javascript
如何使用jquery easyui创建标签组件
2015/11/18 Javascript
微信 java 实现js-sdk 图片上传下载完整流程
2016/10/21 Javascript
JS实现的适合做faq或menu滑动效果示例
2016/11/17 Javascript
解析JavaScript模仿块级作用域
2016/12/29 Javascript
详解如何在Angular中快速定位DOM元素
2017/05/17 Javascript
看看“疫苗查询”小程序有温度的代码
2018/07/31 Javascript
Vue使用NPM方式搭建项目
2018/10/25 Javascript
ES6知识点整理之数组解构和字符串解构的应用示例
2019/04/17 Javascript
vuex根据不同的用户权限展示不同的路由列表功能
2019/09/20 Javascript
vue项目强制清除页面缓存的例子
2019/11/06 Javascript
Node Express用法详解【安装、使用、路由、中间件、模板引擎等】
2020/05/13 Javascript
利用Python的Django框架生成PDF文件的教程
2015/07/22 Python
python实现txt文件格式转换为arff格式
2018/05/31 Python
Python 抓取微信公众号账号信息的方法
2019/06/14 Python
Lenox官网:精美的瓷器&独特的礼品
2017/02/12 全球购物
Booking.com西班牙:全球酒店预订
2018/03/30 全球购物
英语专业学子个人的自我评价
2013/10/02 职场文书
高中生自我评价个人范文
2013/11/09 职场文书
2014年两会学习心得体会
2014/03/17 职场文书
房务中心文员岗位职责
2014/04/16 职场文书
2014年人事部工作总结
2014/12/03 职场文书
自愿离婚协议书2015
2015/01/26 职场文书
2015年幼儿园保育员工作总结
2015/04/23 职场文书
2015年高二班主任工作总结
2015/05/25 职场文书
适合毕业生创业的项目怎么找?
2019/08/08 职场文书
python使用pywinauto驱动微信客户端实现公众号爬虫
2021/05/19 Python