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中数据的批量导入(csv文件)
Oct 09 PHP
WindowsXP中快速配置Apache+PHP5+Mysql
Jun 05 PHP
PHP 截取字符串专题集合
Aug 19 PHP
小文件php+SQLite存储方案
Sep 04 PHP
zen cart新进商品的随机排序修改方法
Sep 10 PHP
php 模拟POST提交的2种方法详解
Jun 17 PHP
关于php程序报date()警告的处理(date_default_timezone_set)
Oct 22 PHP
PHP的变量类型和作用域详解
Mar 12 PHP
JSON用法之将PHP数组转JS数组,JS如何接收PHP数组
Oct 08 PHP
SAE实时日志接口SDK用法示例
Oct 09 PHP
php版微信自定义回复功能示例
Dec 05 PHP
PHP中SESSION过期设置
Mar 09 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
dede全站URL静态化改造[070414更正]
2007/04/17 PHP
PHP中如何调用webservice的实例参考
2013/04/25 PHP
基于php常用正则表达式的整理汇总
2013/06/08 PHP
THINKPHP2.0到3.0有哪些改进之处
2015/01/04 PHP
PHP错误Warning:mysql_query()解决方法
2015/10/24 PHP
php实现的网页版剪刀石头布游戏示例
2016/11/25 PHP
PHP开发的文字水印,缩略图,图片水印实现类与用法示例
2019/04/12 PHP
javascript:history.go()和History.back()的区别及应用
2012/11/25 Javascript
在表单提交前进行验证的几种方式整理
2013/07/31 Javascript
使用js简单实现了tree树菜单
2013/11/20 Javascript
Bootstrap Table使用方法解析
2016/10/19 Javascript
js文件中直接alert()中文出来的是乱码的解决方法
2016/11/01 Javascript
简单实现Bootstrap标签页
2020/08/09 Javascript
js实现键盘自动打字效果
2016/12/23 Javascript
基于angular实现模拟微信小程序swiper组件
2017/06/11 Javascript
VueJs组件prop验证简单介绍
2017/09/12 Javascript
Vue组件中prop属性使用说明实例代码详解
2018/05/31 Javascript
详解使用VueJS开发项目中的兼容问题
2018/08/02 Javascript
详解vue中的父子传值双向绑定及数据更新问题
2019/06/13 Javascript
[01:20]PWL开团时刻DAY9——听说潮汐没用?
2020/11/10 DOTA
Python实现微信公众平台自定义菜单实例
2015/03/20 Python
浅谈Python中chr、unichr、ord字符函数之间的对比
2016/06/16 Python
Python输出指定字符串的方法
2020/02/06 Python
python实现贪吃蛇游戏源码
2020/03/21 Python
Django用户身份验证完成示例代码
2020/04/03 Python
python3中TQDM库安装及使用详解
2020/11/18 Python
纯CSS3制作页面切换效果的实例代码
2019/05/30 HTML / CSS
实例讲解使用SVG制作loading加载动画的方法
2016/04/05 HTML / CSS
详解Canvas 跨域脱坑实践
2018/11/07 HTML / CSS
recorder.js 基于Html5录音功能的实现
2020/05/26 HTML / CSS
美国LOGO设计公司:The Logo Company
2018/07/16 全球购物
波兰最大的宠物用品网上商店:FERA.PL
2019/08/11 全球购物
人力资源职位说明书
2014/07/29 职场文书
大学生暑期实践报告
2015/07/13 职场文书
2016年安全生产先进个人事迹材料
2016/02/29 职场文书
盘点2020年适合农村地区创业的项目
2019/10/16 职场文书