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中static静态变量的使用方法详解
Jun 04 PHP
PHP网站备份程序代码分享
Jun 10 PHP
Memcached常用命令以及使用说明详解
Jun 27 PHP
php Calender(日历)代码分享
Jan 03 PHP
php教程之魔术方法的使用示例(php魔术函数)
Feb 12 PHP
php获取参数的几种方法总结
Feb 18 PHP
PHP+FastCGI+Nginx配置PHP运行环境
Aug 07 PHP
php函数传值的引用传递注意事项分析
Jun 25 PHP
Yii统计不同类型邮箱数量的方法
Oct 18 PHP
thinkPHP5.0框架URL访问方法详解
Mar 18 PHP
PHP实现 APP端微信支付功能
Jun 22 PHP
基于PHP实现用户在线状态检测
Nov 10 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
js 未结束的字符串常量错误解决方法
2010/06/13 Javascript
下载文件个别浏览器文件名乱码解决办法
2013/03/19 Javascript
js常用自定义公共函数汇总
2014/01/15 Javascript
jquery库或JS文件在eclipse下报错问题解决方法
2014/04/17 Javascript
sails框架的学习指南
2014/12/22 Javascript
JS 对象(Object)和字符串(String)互转方法
2016/05/20 Javascript
Javascript表单特效之十大常用原理性样例代码大总结
2016/07/12 Javascript
jQuery+HTML5+CSS3制作支持响应式布局时间轴插件
2016/08/10 Javascript
Bootstrap表单简单实现代码
2017/03/06 Javascript
javascript实现多张图片左右无缝滚动效果
2017/03/22 Javascript
Jquery把获取到的input值转换成json
2017/05/15 jQuery
jQuery获取复选框选中的当前行的某个字段的值
2017/09/15 jQuery
Vue.js实现分页查询功能
2020/11/15 Javascript
nodejs实现的连接MySQL数据库功能示例
2018/01/25 NodeJs
安装Node.js并启动本地服务的操作教程
2018/05/12 Javascript
AngularJS与BootStrap模仿百度分页的示例代码
2018/05/23 Javascript
vue 监听屏幕高度的实例
2018/09/05 Javascript
使用 vue 实例更好的监听事件及vue实例的方法
2019/04/22 Javascript
利用es6 new.target来对模拟抽象类的方法
2019/05/10 Javascript
Js通过AES加密后PHP用Openssl解密的方法
2019/07/12 Javascript
JavaScript判断数据类型有几种方法及区别介绍
2020/09/02 Javascript
对比Python中__getattr__和 __getattribute__获取属性的用法
2016/06/21 Python
python中如何使用朴素贝叶斯算法
2017/04/06 Python
解决在pycharm运行代码,调用CMD窗口的命令运行显示乱码问题
2019/08/23 Python
使用turtle绘制五角星、分形树
2019/10/06 Python
将python依赖包打包成window下可执行文件bat方式
2019/12/26 Python
Python用5行代码实现批量抠图的示例代码
2020/04/14 Python
解决python中0x80072ee2错误的方法
2020/07/19 Python
千禧酒店及度假村官方网站:Millennium Hotels and Resorts
2019/05/10 全球购物
网上商城创业计划书范文
2014/01/31 职场文书
八一演出活动方案
2014/02/03 职场文书
煤矿安全生产责任书
2014/04/15 职场文书
2015年第十五个全民国防教育日宣传活动方案
2015/05/06 职场文书
学生会自荐信
2019/05/16 职场文书
导游词之长城八达岭
2019/09/24 职场文书
Python人工智能之混合高斯模型运动目标检测详解分析
2021/11/07 Python