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 相关文章推荐
xml在joomla表单中的应用详解分享
Jul 19 PHP
基于php常用正则表达式的整理汇总
Jun 08 PHP
解析php php_openssl.dll的作用
Jul 01 PHP
php加速器eAccelerator的配置参数、API详解
May 05 PHP
php查看网页源代码的方法
Mar 13 PHP
php链表用法实例分析
Jul 09 PHP
Zend Framework教程之Zend_Helpers动作助手ViewRenderer用法详解
Jul 20 PHP
PHP编辑器PhpStrom运行缓慢问题
Feb 21 PHP
php实现生成带二维码图片并强制下载功能
Feb 24 PHP
Laravel框架分页实现方法分析
Jun 12 PHP
PHP实现字符串大小写转函数的功能实例
Feb 06 PHP
gearman管理工具GearmanManager的安装与php使用方法示例
Feb 27 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解答方法
2012/02/04 PHP
深入解析php中的foreach函数
2013/08/31 PHP
php调用shell的方法
2014/11/05 PHP
CI(CodeIgniter)模型用法实例分析
2016/01/20 PHP
js使下拉列表框可编辑不止是选择
2013/12/12 Javascript
JavaScript四种调用模式和this示例介绍
2014/01/02 Javascript
js实现Select头像选择实时预览代码
2015/08/17 Javascript
javascript伸缩型菜单实现代码
2015/11/16 Javascript
详解React-Todos入门例子
2016/11/08 Javascript
Bootstrap基本组件学习笔记之缩略图(13)
2016/12/08 Javascript
js/jquery控制页面动态加载数据 滑动滚动条自动加载事件的方法
2017/02/08 Javascript
JavaScript中在光标处插入添加文本标签节点的详细方法
2017/03/22 Javascript
requirejs按需加载angularjs文件实例
2017/06/08 Javascript
解决淘宝cnpm 安装后cnpm不是内部或外部命令的问题
2018/05/17 Javascript
vue和webpack安装命令详解
2018/06/15 Javascript
对angularJs中ng-style动态改变样式的实例讲解
2018/09/30 Javascript
Vue3 源码导读(推荐)
2019/10/14 Javascript
微信小程序实现点击图片放大预览
2019/10/21 Javascript
JS面向对象之单选框实现
2020/01/17 Javascript
python读取csv文件示例(python操作csv)
2014/03/11 Python
python返回昨天日期的方法
2015/05/13 Python
Python找出9个连续的空闲端口
2016/02/01 Python
python中实现迭代器(iterator)的方法示例
2017/01/19 Python
实例分析python3实现并发访问水平切分表
2018/09/29 Python
Python3使用TCP编写一个简易的文件下载器功能
2019/05/08 Python
wxPython实现画图板
2020/08/27 Python
django数据模型(Model)的字段类型解析
2019/12/25 Python
Win系统PyQt5安装和使用教程
2019/12/25 Python
通过实例了解Python异常处理机制底层实现
2020/07/23 Python
Python实现爬取网页中动态加载的数据
2020/08/17 Python
HTML5 视频播放(video),JavaScript控制视频的实例代码
2018/10/08 HTML / CSS
计算机网络毕业生自荐信
2013/10/01 职场文书
酒店总经理欢迎词
2014/01/15 职场文书
给全校老师的建议书
2014/03/13 职场文书
优秀党员学习焦裕禄精神思想汇报范文
2014/09/10 职场文书
蔬果开业典礼发言稿应该怎么写?
2019/09/03 职场文书