加入收藏 | 设为首页 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > 学习资料

PHP实现的mongoDB数据库操作类完整实例

时间:2021-03-21 21:50:18  来源:  作者:

本文实例讲述了PHP实现的mongoDB数据库操作类。分享给大家供大家参考,具体如下:vJj华陈数据科技

最近的项目开发中使用的数据库是mongodb数据库,因为小编的公司也是刚刚使用mongodb数据库,所以之前没有封装好的mongodb数据库操作类拿来使用,所以小编在项目中自己封装了一个mongodb数据库操作类,特拿出来分享,不尽人意的地方希望大家勿喷。vJj华陈数据科技

众所周知,mongodb是典型的nosql数据库的代表,受到很多开发者的追捧,近几年尤为火热,mongodb的流行不是没有原因的,下边给大家简单介绍下MongoDB。vJj华陈数据科技

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。vJj华陈数据科技

它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:vJj华陈数据科技

面向集合存储,易存储对象类型的数据。
模式自由。
支持动态查询。
支持完全索引,包含内部对象。
支持查询。
支持复制和故障恢复。
使用高效的二进制数据存储,包括大型对象(如视频等)。
自动处理碎片,以支持云计算层次的扩展性
支持RUBY,PYTHON,JAVA,C++,PHP等多种语言。
文件存储格式为BSON(一种JSON的扩展)
可通过网络访问
vJj华陈数据科技

所谓“面向集合”(Collenction-Orented),意思是数据被分组存储在数据集中,被称为一个集合(Collenction)。每个 集合在数据库中都有一个唯一的标识名,并且可以包含无限数目的文档。集合的概念类似关系型数据库(RDBMS)里的表(table),不同的是它不需要定 义任何模式(schema)。vJj华陈数据科技

模式自由(schema-free),意味着对于存储在mongodb数据库中的文件,我们不需要知道它的任何结构定义。如果需要的话,你完全可以把不同结构的文件存储在同一个数据库里。vJj华陈数据科技

存储在集合中的文档,被存储为键-值对的形式。键用于唯一标识一个文档,为字符串类型,而值则可以是各中复杂的文件类型。我们称这种存储形式为BSON(Binary Serialized dOcument Format)。vJj华陈数据科技

MongoDB服务端可运行在Linux、Windows或OS X平台,支持32位和64位应用,默认端口为27017。推荐运行在64位平台,因为MongoDBvJj华陈数据科技

在32位模式运行时支持的最大文件尺寸为2GB。vJj华陈数据科技

MongoDB把数据存储在文件中(默认路径为:/data/db),为提高效率使用内存映射文件进行管理。vJj华陈数据科技

小编自己封装的PHP操作MongoDB数据库的数据库操作类源码如下,仅供参考。vJj华陈数据科技

  1. <?php
  2. /**
  3. * PHP操作mongodb数据库操作类
  4. */
  5. class Database {
  6.   protected $database  = '';
  7.   protected $mo;
  8.   /**
  9.    * 构造方法
  10.    */
  11.   public function __construct() {
  12.     $server = DBSERVER;
  13.     $user = DBUSER;
  14.     $password = DBPASS;
  15.     $port = DBPORT;
  16.     $database = DBNAME;
  17.     $mongo = $this->getInstance($server, $user, $password, $port);
  18.     $this->database = $mongo->$database;
  19.   }
  20.   /**
  21.    * 数据库单例方法
  22.    * @param $server
  23.    * @param $user
  24.    * @param $password
  25.    * @param $port
  26.    * @return Mongo
  27.    */
  28.   public function getInstance($server, $user, $password, $port) {
  29.     if (isset($this->mo)) {
  30.       return $this->mo;
  31.     } else {
  32.       if (!empty($server)) {
  33.         if (!empty($port)) {
  34.           if (!empty($user) && !empty($password)) {
  35.             $this->mo = new Mongo("mongodb://{$user}:{$password}@{$server}:{$port}");
  36.           } else {
  37.             $this->mo = new Mongo("mongodb://{$server}:{$port}");
  38.           }
  39.         } else {
  40.           $this->mo = new Mongo("mongodb://{$server}");
  41.         }
  42.       } else {
  43.         $this->mo = new Mongo();
  44.       }
  45.       return $this->mo;
  46.     }
  47.   }
  48.   /**
  49.    * 查询表中所有数据
  50.    * @param $table
  51.    * @param array $where
  52.    * @param array $sort
  53.    * @param string $limit
  54.    * @param string $skip
  55.    * @return array|int
  56.    */
  57.   public function getAll($table, $where = array(), $sort = array(), $limit = '', $skip = '') {
  58.     if (!empty($where)) {
  59.       $data = $this->database->$table->find($where);
  60.     } else {
  61.       $data = $this->database->$table->find();
  62.     }
  63.     if (!empty($sort)) {
  64.       $data = $data->sort($sort);
  65.     }
  66.     if (!empty($limit)) {
  67.       $data = $data->limit($limit);
  68.     }
  69.     if (!empty($skip)) {
  70.       $data = $data->skip($skip);
  71.     }
  72.     $newData = array();
  73.     while ($data->hasNext()) {
  74.       $newData[] = $data->getNext();
  75.     }
  76.     if (count($newData) == 0) {
  77.       return 0;
  78.     }
  79.     return $newData;
  80.   }
  81.   /**
  82.    * 查询指定一条数据
  83.    * @param $table
  84.    * @param array $where
  85.    * @return int
  86.    */
  87.   public function getOne($table, $where = array()) {
  88.     if (!empty($where)) {
  89.       $data = $this->database->$table->findOne($where);
  90.     } else {
  91.       $data = $this->database->$table->findOne();
  92.     }
  93.     return $data;
  94.   }
  95.   /**
  96.    * 统计个数
  97.    * @param $table
  98.    * @param array $where
  99.    * @return mixed
  100.    */
  101.   public function getCount($table, $where = array()) {
  102.     if (!empty($where)) {
  103.       $data = $this->database->$table->find($where)->count();
  104.     } else {
  105.       $data = $this->database->$table->find()->count();
  106.     }
  107.     return $data;
  108.   }
  109.   /**
  110.    * 直接执行mongo命令
  111.    * @param $sql
  112.    * @return array
  113.    */
  114.   public function toExcute($sql) {
  115.     $result = $this->database->execute($sql);
  116.     return $result;
  117.   }
  118.   /**
  119.    * 分组统计个数
  120.    * @param $table
  121.    * @param $where
  122.    * @param $field
  123.    */
  124.   public function groupCount($table, $where, $field) {
  125.     $cond = array(
  126.       array(
  127.         '$match' => $where,
  128.       ),
  129.       array(
  130.         '$group' => array(
  131.           '_id' => '
  132.  
  133. . $field,
  134.           'count' => array('$sum' => 1),
  135.         ),
  136.       ),
  137.       array(
  138.         '$sort' => array("count" => -1),
  139.       ),
  140.     );
  141.     $this->database->$table->aggregate($cond);
  142.   }
  143.   /**
  144.    * 删除数据
  145.    * @param $table
  146.    * @param $where
  147.    * @return array|bool
  148.    */
  149.   public function toDelete($table, $where) {
  150.     $re = $this->database->$table->remove($where);
  151.     return $re;
  152.   }
  153.   /**
  154.    * 插入数据
  155.    * @param $table
  156.    * @param $data
  157.    * @return array|bool
  158.    */
  159.   public function toInsert($table, $data) {
  160.     $re = $this->database->$table->insert($data);
  161.     return $re;
  162.   }
  163.   /**
  164.    * 更新数据
  165.    * @param $table
  166.    * @param $where
  167.    * @param $data
  168.    * @return bool
  169.    */
  170.   public function toUpdate($table, $where, $data) {
  171.     $re = $this->database->$table->update($where, array('$set' => $data));
  172.     return $re;
  173.   }
  174.   /**
  175.    * 获取唯一数据
  176.    * @param $table
  177.    * @param $key
  178.    * @return array
  179.    */
  180.   public function distinctData($table, $key, $query = array()) {
  181.     if (!empty($query)) {
  182.       $where = array('distinct' => $table, 'key' => $key, 'query' => $query);
  183.     } else {
  184.       $where = array('distinct' => $table, 'key' => $key);
  185.     }
  186.     $data = $this->database->command($where);
  187.     return $data['values'];
  188.   }
  189. }
  190. ?>
复制代码

 
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
实现php间隔一段时间执行一次某段代码
实现php间隔一段时间
相关文章
    无相关信息
栏目更新
栏目热门