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面向对象全攻略 (十七) 自动加载类
Sep 30 PHP
php标签云的实现代码
Oct 10 PHP
做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
Apr 13 PHP
一个图片地址分解程序(用于PHP小偷程序)
Aug 23 PHP
PHP提示Warning:phpinfo() has been disabled函数禁用的解决方法
Dec 17 PHP
php通过function_exists检测函数是否存在的方法
Mar 18 PHP
PHP 双链表(SplDoublyLinkedList)简介和使用实例
May 12 PHP
PHP文件生成的图片无法使用CDN缓存的解决方法
Jun 20 PHP
PHP编程开发怎么提高编程效率 提高PHP编程技术
Nov 09 PHP
php中get_magic_quotes_gpc()函数说明
Feb 06 PHP
phpstorm 配置xdebug的示例代码
Mar 31 PHP
php中get_object_vars()在数组的实例用法
Feb 22 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 图片上添加透明度渐变的效果
2009/06/29 PHP
php $_SERVER[&quot;REQUEST_URI&quot;]获取值的通用解决方法
2010/06/21 PHP
php注销代码(session注销)
2012/05/31 PHP
php版淘宝网查询商品接口代码示例
2014/06/17 PHP
php中使用GD库做验证码
2016/03/31 PHP
微信随机生成红包金额算法php版
2016/07/21 PHP
PHP错误和异常处理功能模块示例
2016/11/12 PHP
PHP在linux上执行外部命令的方法
2017/02/06 PHP
基于JavaScript 下namespace 功能的简单分析
2013/07/05 Javascript
Jquery显示和隐藏元素或设为只读(含Ligerui的控件禁用,实例说明介绍)
2013/07/09 Javascript
JQuery教学之性能优化
2014/05/14 Javascript
在JavaScript的jQuery库中操作AJAX的方法讲解
2015/08/15 Javascript
jquery实现的用户注册表单提示操作效果代码分享
2015/08/28 Javascript
jQuery实现可拖拽3D万花筒旋转特效
2017/01/03 Javascript
简单实现nodejs上传功能
2017/01/14 NodeJs
Vue.js之slot深度复制详解
2017/03/10 Javascript
原生javascript实现分页效果
2017/04/21 Javascript
ES6 javascript中class类的get与set用法实例分析
2017/10/30 Javascript
详解如何在webpack中做预渲染降低首屏空白时间
2018/08/22 Javascript
vue弹窗插件实战代码
2018/09/08 Javascript
node.js制作一个简单的登录拦截器
2020/02/10 Javascript
[18:32]DOTA2 HEROS教学视频教你分分钟做大人-谜团
2014/06/12 DOTA
Python内置的字符串处理函数整理
2013/01/29 Python
Python数组条件过滤filter函数使用示例
2014/07/22 Python
实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
2016/01/20 Python
基于django2.2连oracle11g解决版本冲突的问题
2020/07/02 Python
Python爬虫之Selenium设置元素等待的方法
2020/12/04 Python
如何用Python提取10000份log中的产品信息
2021/01/14 Python
用纯css3和html制作泡沫对话框实现代码
2013/03/21 HTML / CSS
eDreams澳大利亚:预订机票、酒店和度假产品
2017/04/19 全球购物
应届毕业生的个人自我鉴定
2013/10/24 职场文书
母亲80寿诞答谢词
2014/01/16 职场文书
低碳环保倡议书
2014/04/14 职场文书
2014年医药代表工作总结
2014/11/22 职场文书
《艾尔登法环》1.03.3补丁上线 碎星伤害调整
2022/04/07 其他游戏
JavaScript原型链中函数和对象的理解
2022/06/16 Javascript