php操作mongodb封装类与用法实例

这篇文章主要介绍了php操作mongodb封装类与用法,结合具体实例形式分析了php定义的MongoDB操作封装类与相关的类实例化、查询、更新等使用技巧,需要的朋友可以参考下。

本文实例讲述了php操作mongodb封装类与用法,分享给大家供大家参考,具体如下:

近来学习了mongodb,刚好是做php开发的,随便写了php操作mongodb的封装类.

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lee
  5. * Date: 2016/10/24
  6. * Time: 13:49
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Http\Common\ReturnApi;
  10. /*
  11. *
  12. *
  13. mongdb常规操作
  14. */
  15. class MongdbCommonController
  16. {
  17. private static $conn;
  18. private static $mon;
  19. private static $error;
  20. private function __construct(){
  21. //self::$conn = new \MongoClient("mongodb://".env('MONGDB_USER').":".env('MONGDB_PASS')."@".env('MONGDB_HOST').":".env('MONGDB_PORT')."/".env('MONGDB_DB'));
  22. self::$conn = new \MongoClient("mongodb://".env('MONGDB_USER').":".env('MONGDB_PASS')."@".env('MONGDB_HOST').":".env('MONGDB_PORT'));
  23. //self::$conn = new \MongoClient("mongodb://".env('MONGDB_HOST').":".env('MONGDB_PORT'));
  24. $db = env('MONGDB_DB');
  25. self::$mon = self::$conn->$db;
  26. }
  27. public static function getInstance(){
  28. if(!(self::$conn instanceof self)){
  29. self::$conn = new self();
  30. }
  31. //return self::$conn->mydb;
  32. return self::$conn;
  33. }
  34. private function __clone(){
  35. trigger_error('Clone is not allowed');
  36. }//禁止克隆
  37. //创建索引
  38. public function ensureIndex($table, $index, $index_param=array())
  39. {
  40. $index_param['safe'] = 1;
  41. try {
  42. self::$mon->$table->ensureIndex($index, $index_param);
  43. return true;
  44. }
  45. catch (MongoCursorException $e)
  46. {
  47. self::$error = $e->getMessage();
  48. return false;
  49. }
  50. }
  51. //添加
  52. public function insert($table,$arr){
  53. try {
  54. self::$mon->$table->insert($arr, array('w'=>true));
  55. return true;
  56. }
  57. catch (MongoCursorException $e)
  58. {
  59. self::$error = $e->getMessage();
  60. return false;
  61. }
  62. }
  63. //更新
  64. public function update($table, $condition, $new_arr, $options=array())
  65. {
  66. $options['w'] = 1;
  67. if (!isset($options['multiple']))
  68. {
  69. $options['multiple'] = 0;
  70. }
  71. try {
  72. self::$mon->$table->update($condition, $new_arr, $options);
  73. return true;
  74. }
  75. catch (MongoCursorException $e)
  76. {
  77. self::$error = $e->getMessage();
  78. return false;
  79. }
  80. }
  81. //删除
  82. public function remove($table, $condition, $options=array())
  83. {
  84. $options['w'] = 1;
  85. try {
  86. self::$mon->$table->remove($condition, $options);
  87. return true;
  88. }
  89. catch (MongoCursorException $e)
  90. {
  91. self::$error = $e->getMessage();
  92. return false;
  93. }
  94. }
  95. //查找
  96. public function find($table, $query_condition, $result_condition=array(), $fields=array())
  97. {
  98. $cursor = self::$mon->$table->find($query_condition, $fields);
  99. if (!emptyempty($result_condition['start']))
  100. {
  101. $cursor->skip($result_condition['start']);
  102. }
  103. if (!emptyempty($result_condition['limit']))
  104. {
  105. $cursor->limit($result_condition['limit']);
  106. }
  107. if (!emptyempty($result_condition['sort']))
  108. {
  109. $cursor->sort($result_condition['sort']);
  110. }
  111. $result = array();
  112. try {
  113. while ($cursor->hasNext())
  114. {
  115. $result[] = $cursor->getNext();
  116. }
  117. }
  118. catch (MongoCursorTimeoutException $e)
  119. {
  120. self::$error = $e->getMessage();
  121. return false;
  122. }
  123. return $result;
  124. }
  125. //查找一条记录
  126. public function findOne($table, $condition, $fields=array())
  127. {
  128. return self::$mon->$table->findOne($condition, $fields);
  129. }
  130. //返回表的记录数
  131. public function count($table)
  132. {
  133. return self::$mon->$table->count();
  134. }
  135. //返回错误信息
  136. public function getError()
  137. {
  138. return self::$error;
  139. }
  140. }

操作实例:

  1. use App\Http\Controllers\Api\MongdbCommonController;
  2. $db = MongdbCommonController::getInstance();
  3. $collection = 'tab';
  4. $data = array('tt' =>'sdsd',
  5. 'pp' => 'ssdsdf');
  6. //返回记录数
  7. echo $db->count($collection);
  8. //插入记录
  9. $db->insert($collection, array("id"=>2, "title"=>"asdqw"));
  10. //更新
  11. $db->update($collection, array("id"=>2),array('tt'=>'dfdfd',"gg"=>"bbb",'hh'=>'dfsdsd'));
  12. //查找记录
  13. echo '<pre>';
  14. print_r( $db->find($collection, array("tt"=>'dfdfd'), array("start"=>1,"limit"=>4)));
  15. //删除
  16. $db->remove($collection, array('tt' =>'sdsd'));