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 相关文章推荐
967 个函式
Oct 09 PHP
php注入实例
Oct 09 PHP
PHP 上传文件大小限制
Jul 05 PHP
在PHP中设置、使用、删除Cookie的解决方法
May 06 PHP
php 表单提交大量数据发生丢失的解决方法
Mar 03 PHP
php提交post数组参数实例分析
Dec 17 PHP
ThinkPHP框架里隐藏index.php
Apr 12 PHP
phpcms中的评论样式修改方法
Oct 21 PHP
php写app接口并返回json数据的实例(分享)
May 20 PHP
Yii2第三方类库插件Imagine的安装和使用
Jul 06 PHP
PHP数组去重的更快实现方式分析
May 09 PHP
php依赖注入知识点详解
Sep 23 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/10/21 PHP
php设计模式 Chain Of Responsibility (职责链模式)
2011/06/26 PHP
Yii框架学习笔记之session与cookie简单操作示例
2019/04/30 PHP
jQuery 连续列表实现代码
2009/12/21 Javascript
jQuery 获取对象 基本选择与层级
2010/05/31 Javascript
jQuery1.3.2 升级到jQuery1.4.4需要修改的地方
2011/01/06 Javascript
简单的前端js+ajax 购物车框架(入门篇)
2011/10/29 Javascript
JavaScript动态修改弹出窗口大小的方法
2015/04/06 Javascript
JavaScript学习笔记之检测客户端类型是(引擎、浏览器、平台、操作系统、移动设备)
2015/12/03 Javascript
Bootstrap模态框(Modal)实现过渡效果
2017/03/17 Javascript
jQuery插件FusionCharts实现的3D帕累托图效果示例【附demo源码】
2017/03/25 jQuery
VUE利用vuex模拟实现新闻点赞功能实例
2017/06/28 Javascript
Nodejs 复制文件/文件夹的方法
2017/08/24 NodeJs
Javascript模拟实现new原理解析
2020/03/03 Javascript
js简单粗暴的发布订阅示例代码
2021/01/23 Javascript
[01:32]寻找你心中的那团火 DOTA2 TI9火焰传递活动今日开启
2019/05/16 DOTA
Python里disconnect UDP套接字的方法
2015/04/23 Python
pip install urllib2不能安装的解决方法
2018/06/12 Python
浅谈关于Python3中venv虚拟环境
2018/08/01 Python
Django如何使用jwt获取用户信息
2020/04/21 Python
如何将tensorflow训练好的模型移植到Android (MNIST手写数字识别)
2020/04/22 Python
PyTorch-GPU加速实例
2020/06/23 Python
详解css3中dispaly的Grid布局与Flex布局
2020/09/11 HTML / CSS
基于HTML5超酷摄像头(HTML5 webcam)拍照功能实现代码
2012/12/13 HTML / CSS
多视角3D可旋转的HTML5 Logo动画
2016/03/02 HTML / CSS
国外的一些J2EE面试题一
2012/10/13 面试题
中学教师实习自我鉴定
2013/09/28 职场文书
员工趣味活动方案
2014/08/27 职场文书
2015年初中元旦晚会活动总结
2014/11/28 职场文书
师德承诺书
2015/01/20 职场文书
催款通知书范文
2015/04/17 职场文书
2015法院个人工作总结范文
2015/05/25 职场文书
在职证明范本
2015/06/15 职场文书
2016年村干部公开承诺书(公开承诺事项)
2016/03/25 职场文书
Python爬取某拍短视频
2021/06/11 Python
开发者首先否认《遗弃》被取消的传言
2022/04/11 其他游戏