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(2)
Oct 09 PHP
谈谈PHP语法(2)
Oct 09 PHP
BBS(php &amp; mysql)完整版(八)
Oct 09 PHP
php zend解密软件绿色版测试可用
Apr 14 PHP
PHP 读取文件的正确方法
Apr 29 PHP
php in_array 函数使用说明与in_array需要注意的地方说明
Apr 13 PHP
php 在文件指定行插入数据的代码
May 08 PHP
实例讲解php数据访问
May 09 PHP
ThinkPHP打水印及设置水印位置的方法
Oct 14 PHP
PHP实现求解最长公共子串问题的方法
Nov 17 PHP
PHP实现微信提现功能(微信商城)
Nov 21 PHP
基于PHP的微信公众号的开发流程详解
Aug 07 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
使用gd库实现php服务端图片裁剪和生成缩略图功能分享
2013/12/25 PHP
thinkPHP批量删除的实现方法分析
2016/11/09 PHP
PHP7新特性之抽象语法树(AST)带来的变化详解
2018/07/17 PHP
Javascript typeof 用法
2008/12/28 Javascript
Javascript 面向对象编程(coolshell)
2012/03/18 Javascript
圣诞节Merry Christmas给博客添加浪漫的下雪效果基于jquery实现
2012/12/27 Javascript
js 验证身份证信息有效性
2014/03/28 Javascript
jQuery实现html表格动态添加新行的方法
2015/05/28 Javascript
在AngularJS应用中实现一些动画效果的代码
2015/06/18 Javascript
jfinal与bootstrap的登录跳转实战演习
2015/09/22 Javascript
Bootstrap实现弹性搜索框
2016/07/11 Javascript
Js动态设置rem来实现移动端字体的自适应代码
2016/10/14 Javascript
Node连接mysql数据库方法介绍
2017/02/07 Javascript
原生JavaScript实现todolist功能
2018/03/02 Javascript
layui加载数据显示loading加载完成loading消失的实例代码
2019/09/23 Javascript
[01:11]回顾历届DOTA2国际邀请赛中国区预选赛
2017/06/26 DOTA
[02:21]2018完美盛典章节片——初心
2018/12/17 DOTA
[01:59][TI9趣味视频] 全明星赛奖励
2019/08/23 DOTA
python实现跨文件全局变量的方法
2014/07/07 Python
python 删除大文件中的某一行(最有效率的方法)
2017/08/19 Python
Django如何开发简单的查询接口详解
2019/05/17 Python
python如何给字典的键对应的值为字典项的字典赋值
2019/07/05 Python
Python运行提示缺少模块问题解决方案
2020/04/02 Python
python 密码学示例——理解哈希(Hash)算法
2020/09/21 Python
css3 transform 3d 使用css3创建动态3d立方体(html5实践)
2013/01/06 HTML / CSS
shell程序中如何注释
2012/01/28 面试题
美术专业学生个人自我评价
2013/09/19 职场文书
旅游与酒店管理专业求职信
2014/07/21 职场文书
师德标兵事迹材料
2014/12/19 职场文书
维稳承诺书
2015/01/20 职场文书
党风廉正建设个人工作总结
2015/03/06 职场文书
运动员加油词
2015/07/18 职场文书
2015大一新生军训感言
2015/08/01 职场文书
教师信息技术学习心得体会
2016/01/21 职场文书
《彼得与狼》教学反思
2016/02/20 职场文书
2019年个人工作总结范文
2019/03/25 职场文书