PHP操作mongodb数据库操作类

来源:互联网 发布:哪个网站股票准确数据 编辑:程序博客网 时间:2024/05/14 09:04

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

众所周知,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'];    }}?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207

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


来源:http://blog.csdn.net/u010957293/article/details/51942773