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 08 PHP
ThinkPHP关联模型操作实例分析
Sep 23 PHP
Laravel中使用自己编写类库的3种方法
Feb 10 PHP
php生成年月日下载列表的方法
Apr 24 PHP
PHP的命令行命令使用指南
Aug 18 PHP
详解PHP的Yii框架中日志的相关配置及使用
Dec 08 PHP
Symfony学习十分钟入门经典教程
Feb 03 PHP
Yii2框架BootStrap样式的深入理解
Nov 07 PHP
PHP Mysqli 常用代码集合
Nov 12 PHP
ThinkPHP使用getlist方法实现数据搜索功能示例
May 08 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
Nov 12 PHP
Yii实现微信公众号场景二维码的方法实例
Aug 30 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
咖啡历史、消费和行业趋势
2021/03/03 咖啡文化
ThinkPHP3.1新特性之Action参数绑定
2014/06/19 PHP
ThinkPHP使用smarty模板引擎的方法
2014/07/01 PHP
PHP中isset与array_key_exists的区别实例分析
2015/06/02 PHP
基于PHP+jQuery+MySql实现红蓝(顶踩)投票代码
2015/08/25 PHP
php版微信公众平台入门教程之开发者认证的方法
2016/09/26 PHP
Yii2――使用数据库操作汇总(增删查改、事务)
2016/12/19 PHP
php-app开发接口加密详解
2018/04/18 PHP
原生js写的放大镜效果
2012/08/22 Javascript
鼠标左键单击冲突的问题解决方法(防止冒泡)
2014/05/14 Javascript
JavaScript strike方法入门实例(给字符串加上删除线)
2014/10/17 Javascript
兼容各大浏览器的JavaScript阻止事件冒泡代码
2015/07/09 Javascript
javascript实现类似百度分享功能的方法
2015/07/27 Javascript
Bootstrap零基础入门教程(三)
2016/07/18 Javascript
jquery实现点击页面回到顶部
2016/11/23 Javascript
angularjs ocLazyLoad分步加载js文件实例
2017/01/17 Javascript
js实现表格筛选功能
2017/01/18 Javascript
微信小程序loading组件显示载入动画用法示例【附源码下载】
2017/12/09 Javascript
vue+vue-router转场动画的实例代码
2018/09/01 Javascript
js tab栏切换代码实例解析
2019/09/03 Javascript
mpvue实现微信小程序快递单号查询代码
2020/04/03 Javascript
Python输出9*9乘法表的方法
2015/05/25 Python
Python 基于Twisted框架的文件夹网络传输源码
2016/08/28 Python
Python连接SQLServer2000的方法详解
2017/04/19 Python
python实现抖音视频批量下载
2018/06/20 Python
python 利用浏览器 Cookie 模拟登录的用户访问知乎的方法
2019/07/11 Python
python实现小世界网络生成
2019/11/21 Python
python自动生成证件号的方法示例
2021/01/14 Python
菲律宾最大的网上花店和礼品店:PhilFlower.com
2018/02/09 全球购物
人力资源经理的岗位职责范本
2014/02/28 职场文书
合伙经营协议书范本
2014/04/18 职场文书
超市商业计划书
2014/05/04 职场文书
幽默自我介绍演讲稿
2014/08/21 职场文书
卡特教练观后感
2015/06/08 职场文书
《迟到》教学反思
2016/02/24 职场文书
CentOS8.4安装Redis6.2.6的详细过程
2021/11/20 Redis