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基础学习笔记
Mar 18 PHP
php in_array 函数使用说明与in_array需要注意的地方说明
Apr 13 PHP
PHP中使用gettext来支持多语言的方法
May 02 PHP
php入门学习知识点八 PHP中for循环基本应用之九九乘法口绝表
Jul 14 PHP
PHP获取url的函数代码
Aug 02 PHP
PHP排序之二维数组的按照字母排序实现代码
Aug 13 PHP
php数组函数序列之array_search()- 按元素值返回键名
Nov 04 PHP
百度站点地图(百度sitemap)生成方法分享
Jan 09 PHP
PHP获取指定月份第一天和最后一天的方法
Jul 18 PHP
PHP中new static()与new self()的比较
Aug 19 PHP
浅谈PHP拦截器之__set()与__get()的理解与使用方法
Oct 18 PHP
Laravel5.5新特性之友好报错以及展示详解
Aug 13 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运行环境配置的详解
2013/06/04 PHP
Drupal读取Excel并导入数据库实例
2014/03/02 PHP
ThinkPHP的截取字符串函数无法显示省略号的解决方法
2014/06/25 PHP
PHP静态文件生成类实例
2014/11/29 PHP
PHP如何通过AJAX方式实现登录功能
2015/11/23 PHP
[原创]php实现子字符串位置相互对调互换的方法
2016/06/02 PHP
jQuery 表单验证扩展(三)
2010/10/20 Javascript
javascipt匹配单行和多行注释的正则表达式
2013/11/20 Javascript
JSON.stringify转换JSON时日期时间不准确的解决方法
2014/08/08 Javascript
css如何让浮动元素水平居中
2015/08/07 Javascript
AngularJS 让人爱不释手的八种功能
2016/03/23 Javascript
Actionscript与javascript交互实例程序(修改)
2016/09/22 Javascript
vue 和vue-touch 实现移动端左右导航效果(仿京东移动站导航)
2017/04/22 Javascript
AngularJS使用ng-class动态增减class样式的方法示例
2017/05/18 Javascript
ajax +NodeJS 实现图片上传实例
2017/06/06 NodeJs
JavaScript表单即时验证 验证不成功不能提交
2017/08/31 Javascript
Three.js如何用轨迹球插件(trackball)增加对模型的交互功能详解
2017/09/25 Javascript
ionic3双击返回退出应用的方法
2019/09/17 Javascript
vue实现购物车案例
2020/05/30 Javascript
浅析JavaScript预编译和暗示全局变量
2020/09/03 Javascript
ant design中upload组件上传大文件,显示进度条进度的实例
2020/10/29 Javascript
关于Vue中$refs的探索浅析
2020/11/05 Javascript
Antd-vue Table组件添加Click事件,实现点击某行数据教程
2020/11/17 Javascript
python读取浮点数和读取文本文件示例
2014/05/06 Python
Python使用xlrd模块操作Excel数据导入的方法
2015/05/26 Python
Django接受前端数据的几种方法总结
2016/11/04 Python
pandas数据预处理之dataframe的groupby操作方法
2018/04/13 Python
python3.5绘制随机漫步图
2018/08/27 Python
python 获取sqlite3数据库的表名和表字段名的实例
2019/07/17 Python
英国最大的奢侈珠宝和手表网站:C W Sellors
2017/02/10 全球购物
台湾网友喜爱的综合型网路购物商城:Yahoo! 奇摩购物中心
2018/03/10 全球购物
软件设计的目标是什么
2016/12/04 面试题
新年主持词
2014/03/27 职场文书
服务宗旨标语
2014/07/01 职场文书
初中生考试作弊检讨书
2014/12/14 职场文书
CSS实现五种常用的2D转换
2021/12/06 HTML / CSS