PHP操作mongodb数据库操作类

来源:互联网 发布:秦素妍捏脸数据 编辑:程序博客网 时间:2024/06/08 13:32

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

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

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

面向集合存储,易存储对象类型的数据。

模式自由。

支持动态查询。

支持完全索引,包含内部对象。

支持查询。

支持复制和故障恢复。

使用高效的二进制数据存储,包括大型对象(如视频等)。

自动处理碎片,以支持云计算层次的扩展性

支持RUBY,Python,Java,C++,PHP等多种语言。

文件存储格式为BSON(一种JSON的扩展)

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

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

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

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

在32位模式运行时支持的最大文件尺寸为2GB。

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

小编自己封装的php操作MongoDB数据库的数据库操作类源码如下,仅供参考。

<?php/** * PHP操作mongodb数据库操作类 */class Database {    protected $database    = '';    protected $mo;    /**     * 构造方法     */    public function __construct() {        $server = DBSERVER;        $user = DBUSER;        $password = DBPASS;        $port = DBPORT;        $database = DBNAME;        $mongo = $this->getInstance($server, $user, $password, $port);        $this->database = $mongo->$database;    }    /**     * 数据库单例方法     * @param $server     * @param $user     * @param $password     * @param $port     * @return Mongo     */    public function getInstance($server, $user, $password, $port) {        if (isset($this->mo)) {            return $this->mo;        } else {            if (!empty($server)) {                if (!empty($port)) {                    if (!empty($user) && !empty($password)) {                        $this->mo = new Mongo("mongodb://{$user}:{$password}@{$server}:{$port}");                    } else {                        $this->mo = new Mongo("mongodb://{$server}:{$port}");                    }                } else {                    $this->mo = new Mongo("mongodb://{$server}");                }            } else {                $this->mo = new Mongo();            }            return $this->mo;        }    }    /**     * 查询表中所有数据     * @param $table     * @param array $where     * @param array $sort     * @param string $limit     * @param string $skip     * @return array|int     */    public function getAll($table, $where = array(), $sort = array(), $limit = '', $skip = '') {        if (!empty($where)) {            $data = $this->database->$table->find($where);        } else {            $data = $this->database->$table->find();        }        if (!empty($sort)) {            $data = $data->sort($sort);        }        if (!empty($limit)) {            $data = $data->limit($limit);        }        if (!empty($skip)) {            $data = $data->skip($skip);        }        $newData = array();        while ($data->hasNext()) {            $newData[] = $data->getNext();        }        if (count($newData) == 0) {            return 0;        }        return $newData;    }    /**     * 查询指定一条数据     * @param $table     * @param array $where     * @return int     */    public function getOne($table, $where = array()) {        if (!empty($where)) {            $data = $this->database->$table->findOne($where);        } else {            $data = $this->database->$table->findOne();        }        return $data;    }    /**     * 统计个数     * @param $table     * @param array $where     * @return mixed     */    public function getCount($table, $where = array()) {        if (!empty($where)) {            $data = $this->database->$table->find($where)->count();        } else {            $data = $this->database->$table->find()->count();        }        return $data;    }    /**     * 直接执行mongo命令     * @param $sql     * @return array     */    public function toExcute($sql) {        $result = $this->database->execute($sql);        return $result;    }    /**     * 分组统计个数     * @param $table     * @param $where     * @param $field     */    public function groupCount($table, $where, $field) {        $cond = array(            array(                '$match' => $where,            ),            array(                '$group' => array(                    '_id' => '$' . $field,                    'count' => array('$sum' => 1),                ),            ),            array(                '$sort' => array("count" => -1),            ),        );        $this->database->$table->aggregate($cond);    }    /**     * 删除数据     * @param $table     * @param $where     * @return array|bool     */    public function toDelete($table, $where) {        $re = $this->database->$table->remove($where);        return $re;    }    /**     * 插入数据     * @param $table     * @param $data     * @return array|bool     */    public function toInsert($table, $data) {        $re = $this->database->$table->insert($data);        return $re;    }    /**     * 更新数据     * @param $table     * @param $where     * @param $data     * @return bool     */    public function toUpdate($table, $where, $data) {        $re = $this->database->$table->update($where, array('$set' => $data));        return $re;    }    /**     * 获取唯一数据     * @param $table     * @param $key     * @return array     */    public function distinctData($table, $key, $query = array()) {        if (!empty($query)) {            $where = array('distinct' => $table, 'key' => $key, 'query' => $query);        } else {            $where = array('distinct' => $table, 'key' => $key);        }        $data = $this->database->command($where);        return $data['values'];    }}?>

转自:http://www.sgphp.com/articles/55dfd799eabc8875738b45d5.html

原创粉丝点击