当前位置:Gxlcms > PHP教程 > PHP+mongoDB数据库操作步骤详解

PHP+mongoDB数据库操作步骤详解

时间:2021-07-01 10:21:17 帮助过:35人阅读

这次给大家带来PHP+mongoDB数据库操作步骤详解,PHP+mongoDB数据库的注意事项有哪些,下面就是实战案例,一起来看一下。

最近的项目开发中使用的数据库是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),为提高效率使用内存映射文件进行管理。

  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' => '$' . $field,
  132. 'count' => array('$sum' => 1),
  133. ),
  134. ),
  135. array(
  136. '$sort' => array("count" => -1),
  137. ),
  138. );
  139. $this->database->$table->aggregate($cond);
  140. }
  141. /**
  142. * 删除数据
  143. * @param $table
  144. * @param $where
  145. * @return array|bool
  146. */
  147. public function toDelete($table, $where) {
  148. $re = $this->database->$table->remove($where);
  149. return $re;
  150. }
  151. /**
  152. * 插入数据
  153. * @param $table
  154. * @param $data
  155. * @return array|bool
  156. */
  157. public function toInsert($table, $data) {
  158. $re = $this->database->$table->insert($data);
  159. return $re;
  160. }
  161. /**
  162. * 更新数据
  163. * @param $table
  164. * @param $where
  165. * @param $data
  166. * @return bool
  167. */
  168. public function toUpdate($table, $where, $data) {
  169. $re = $this->database->$table->update($where, array('$set' => $data));
  170. return $re;
  171. }
  172. /**
  173. * 获取唯一数据
  174. * @param $table
  175. * @param $key
  176. * @return array
  177. */
  178. public function distinctData($table, $key, $query = array()) {
  179. if (!empty($query)) {
  180. $where = array('distinct' => $table, 'key' => $key, 'query' => $query);
  181. } else {
  182. $where = array('distinct' => $table, 'key' => $key);
  183. }
  184. $data = $this->database->command($where);
  185. return $data['values'];
  186. }
  187. }
  188. ?>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

php微信公众号随机发放现金红包功能

PHP实现负载均衡下的session共用案列详解(附代码)

以上就是PHP+mongoDB数据库操作步骤详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行