免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1658 | 回复: 0
打印 上一主题 下一主题

php实现的mongodb操作类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-20 13:06 |只看该作者 |倒序浏览
mongo_db.php
  1. <?php

  2. /**
  3. * Created by PhpStorm.
  4. * User: yangyulong
  5. * Date: 2015/5/26
  6. * Time: 13:45
  7. */
  8. class Mongo_db
  9. {
  10.     private static $instanceof = NULL;
  11.     public $mongo;
  12.     private $host = 'localhost';
  13.     private $port = '27017';

  14.     private $db;
  15.     public $dbname;
  16.     private $table = NULL;

  17.     /**
  18.      * 初始化类,得到mongo的实例对象
  19.      */
  20.     public function __construct($host = NULL, $port = NULL, $dbname = NULL, $table = NULL)
  21.     {

  22.         if (NULL === $dbname) {
  23.             $this->throwError('集合不能为空!');
  24.         }

  25.         //判断是否传递了host和port
  26.         if (NULL !== $host) {
  27.             $this->host = $host;
  28.         }

  29.         if (NULL !== $port) {
  30.             $this->port = $port;
  31.         }

  32.         $this->table = $table;

  33.         $this->mongo = new MongoClient($this->host . ':' . $this->port);
  34.         if ($this->getVersion() >= '0.9.0') {
  35.             $this->dbname = $this->mongo->selectDB($dbname);
  36.             $this->db = $this->dbname->selectCollection($table);
  37.         } else {
  38.             $this->db = $this->mongo->$dbname->$table;
  39.         }
  40.     }

  41.     public function getVersion()
  42.     {
  43.         return MongoClient::VERSION;
  44.     }

  45.     /**
  46.      * 单例模式
  47.      * @return Mongo|null
  48.      */
  49.     //public static function getInstance($host=null, $port=null, $dbname=null, $table=null){
  50.     //
  51.     //    if(!(self::$instanceof instanceof self)){
  52.     //        self::$instanceof = new self($host, $port, $dbname, $table);
  53.     //    }
  54.     //
  55.     //    return self::$instanceof;
  56.     //}

  57.     /**
  58.      * 插入一条数据
  59.      * @param array $doc
  60.      */
  61.     public function insert($doc = array())
  62.     {
  63.         if (empty($doc)) {
  64.             $this->throwError('插入的数据不能为空!');
  65.         }
  66.         //保存数据信息
  67.         try {
  68.             if (!$this->db->insert($doc)) {
  69.                 throw new MongoException('插入数据失败');
  70.             }
  71.         } catch (MongoException $e) {
  72.             $this->throwError($e->getMessage());
  73.         }
  74.     }

  75.     /**
  76.      * 插入多条数据信息
  77.      * @param array $doc
  78.      */
  79.     public function insertMulti($doc = array())
  80.     {
  81.         if (empty($doc)) {
  82.             $this->throwError('插入的数据不能为空!');
  83.         }
  84.         //插入数据信息
  85.         foreach ($doc as $key => $val) {
  86.             //判断$val是不是数组
  87.             if (is_array($val)) {
  88.                 $this->insert($val);
  89.             }
  90.         }
  91.     }

  92.     /**
  93.      * 查找一条记录
  94.      * @return array|null
  95.      */
  96.     public function findOne($where = NULL)
  97.     {
  98.         if (NULL === $where) {
  99.             try {
  100.                 if ($result = $this->db->findOne()) {
  101.                     return $result;
  102.                 } else {
  103.                     throw new MongoException('查找数据失败');
  104.                 }
  105.             } catch (MongoException $e) {
  106.                 $this->throwError($e->getMessage());
  107.             }
  108.         } else {
  109.             try {
  110.                 if ($result = $this->db->findOne($where)) {
  111.                     return $result;
  112.                 } else {
  113.                     throw new MongoException('查找数据失败');
  114.                 }
  115.             } catch (MongoException $e) {
  116.                 $this->throwError($e->getMessage());
  117.             }
  118.         }

  119.     }

  120.     /**
  121.      * todo 带条件的随后做
  122.      * 查找所有的文档
  123.      * @return MongoCursor
  124.      */
  125.     public function find($where = NULL)
  126.     {
  127.         if (NULL === $where) {

  128.             try {
  129.                 if ($result = $this->db->find()) {

  130.                 } else {
  131.                     throw new MongoException('查找数据失败');
  132.                 }
  133.             } catch (MongoException $e) {
  134.                 $this->throwError($e->getMessage());
  135.             }
  136.         } else {
  137.             try {
  138.                 if ($result = $this->db->find($where)) {

  139.                 } else {
  140.                     throw new MongoException('查找数据失败');
  141.                 }
  142.             } catch (MongoException $e) {
  143.                 $this->throwError($e->getMessage());
  144.             }
  145.         }

  146.         $arr = array();
  147.         foreach ($result as $id => $val) {
  148.             $arr[] = $val;
  149.         }

  150.         return $arr;
  151.     }

  152.     /**
  153.      * 获取记录条数
  154.      * @return int
  155.      */
  156.     public function getCount()
  157.     {
  158.         try {
  159.             if ($count = $this->db->count()) {
  160.                 return $count;
  161.             } else {
  162.                 throw new MongoException('查找总数失败');
  163.             }
  164.         } catch (MongoException $e) {
  165.             $this->throwError($e->getMessage());
  166.         }
  167.     }

  168.     /**
  169.      * 获取所有的数据库
  170.      * @return array
  171.      */
  172.     public function getDbs()
  173.     {
  174.         return $this->mongo->listDBs();
  175.     }

  176.     /**
  177.      * 删除数据库
  178.      * @param null $dbname
  179.      * @return mixed
  180.      */
  181.     public function dropDb($dbname = NULL)
  182.     {
  183.         if (NULL !== $dbname) {
  184.             $retult = $this->mongo->dropDB($dbname);
  185.             if ($retult['ok']) {
  186.                 return TRUE;
  187.             } else {
  188.                 return FALSE;
  189.             }
  190.         }
  191.         $this->throwError('请输入要删除的数据库名称');
  192.     }

  193.     /**
  194.      * 强制关闭数据库的链接
  195.      */
  196.     public function closeDb()
  197.     {
  198.         $this->mongo->close(TRUE);
  199.     }

  200.     /**
  201.      * 输出错误信息
  202.      * @param $errorInfo 错误内容
  203.      */
  204.     public function throwError($errorInfo='')
  205.     {
  206.         echo "<h3>出错了:$errorInfo</h3>";
  207.         die();
  208.     }

  209. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP