PHP MongoDB 操作类源码分享
本示例详细解析了由 PHP 编写的 MongoDB 数据库操作类,提供完整代码供开发者参考与借鉴。
在当前项目开发中采用了 MongoDB 作为数据存储方案。鉴于团队刚引入该技术栈,尚未有现成的封装库可用,因此作者在项目中自行构建了一套 MongoDB 操作类,并在此公开分享,不足之处恳请指正。
MongoDB 作为 NoSQL 数据库的典型代表,近年来备受开发者青睐。其流行并非偶然,以下将对 MongoDB 的核心概念进行简要介绍。
MongoDB 是一款介于关系型与非关系型数据库之间的产品,在非关系型数据库中具备最丰富的功能,且最接近关系型数据库的体验。它采用类似 JSON 的 BSON 格式存储数据,结构松散,能够容纳复杂的数据类型。其查询语言强大且语法类似面向对象查询,几乎能实现关系型数据库单表查询的大部分功能,同时支持建立索引。
该数据库以高性能、易部署和易用性著称,数据存储便捷。其主要功能特性包括:
面向集合存储,便于对象类型数据的存取。 支持模式自由(schema-free)。 具备动态查询能力。 支持完全索引,涵盖内部对象。 提供强大的查询支持。 支持复制机制与故障恢复。 采用高效的二进制数据存储,适合大型对象(如视频)。 自动处理碎片,支持云计算层面的扩展。 兼容 RUBY、PYTHON、JAVA、C++、PHP 等多种编程语言。 文件存储格式为 BSON(JSON 的扩展)。 可通过网络进行访问。
"面向集合"(Collection-Oriented)指数据被分组存储在称为“集合”的数据集中。每个集合在数据库中拥有唯一标识名,可包含无限数量的文档。集合概念类似于关系型数据库中的表,但无需定义任何模式(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'];
}
}
?>
希望上述内容能对 PHP 程序设计人员有所帮助。