MongoUtil.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace loyalsoft;
  3. /*
  4. * File:MongoUtil.php
  5. * Function: MongoDB operation helper.
  6. * Author: gwang(wanggangzero@qq.com)
  7. * Version:
  8. * 1.0.0 Creat file. --gwang 2022.2.14
  9. */
  10. /**
  11. * MongoDB辅助类
  12. * Author: gwang(wanggangzero@qq.com)
  13. */
  14. class MongoUtil {
  15. /**
  16. * 负责与MongoDB连接, 管理
  17. * @var \MongoDB\Driver\Manager
  18. */
  19. private $mc;
  20. /**
  21. * 返回一个新的option构造器,请使用链式写法追加所有option, 最后用GetOption()获取最终的option数组
  22. * @author gwang <wanggangzero@qq.com>
  23. * @return \loyalsoft\MongoQueryOptionBuilder
  24. */
  25. public static function QueryOptionBuilder() {
  26. return new MongoQueryOptionBuilder();
  27. }
  28. /**
  29. *
  30. * @param type $connStr
  31. * @return \MongoDB\Driver\Manager|false
  32. */
  33. public function conn($connStr) {
  34. try {
  35. $this->mc = new \MongoDB\Driver\Manager($connStr);
  36. return $this->mc;
  37. } catch (\Exception $e) {
  38. Clog::err($e->getMessage());
  39. return false;
  40. }
  41. }
  42. /**
  43. * 查询
  44. * @author gwang<wanggangzero@qq.com>
  45. * @param string $collection 集合
  46. * @param array $filter
  47. * @param array $options
  48. * @return \MongoDB\Driver\Cursor|false
  49. */
  50. public function find($collection, $filter = [], $options = []) {
  51. if (empty($this->mc)) {
  52. return false;
  53. }
  54. try {
  55. $query = new \MongoDB\Driver\Query($filter, $options);
  56. $cursor = $this->mc->executeQuery($collection, $query);
  57. return $cursor;
  58. } catch (\Exception $ex) {
  59. CLog::err($ex->getMessage());
  60. return false;
  61. }
  62. }
  63. /**
  64. * 插入1条数据
  65. * @param string $collection 表名(db.collection)
  66. * @param array $addArr 待插入的数据,要求是数组
  67. * @return boolean
  68. */
  69. public function insert($collection, $addArr) {
  70. if (empty($addArr) || !is_array($addArr)) {
  71. return false;
  72. }
  73. if (empty($this->mc)) {
  74. return false;
  75. }
  76. try {
  77. $bulk = new \MongoDB\Driver\BulkWrite();
  78. $bulk->insert($addArr);
  79. $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 6000);
  80. $result = $this->mc->executeBulkWrite($collection, $bulk, $writeConcern);
  81. if ($result->getInsertedCount()) {
  82. return true;
  83. }
  84. } catch (\Exception $e) {
  85. CLog::err($e->getMessage()); # 记录错误日志
  86. }
  87. return false;
  88. }
  89. /**
  90. * 删除
  91. * @param string $collection 表名
  92. * @param array $whereArr 筛选条件
  93. * @param bool $limit 是否仅删除第一条(默认false,删除所有匹配条目)
  94. * @return boolean
  95. */
  96. public function delete($collection, $whereArr, $limit = false) {
  97. if (empty($whereArr)) {
  98. return false;
  99. }
  100. $options = ['limit' => $limit];
  101. $conn = $this->mc;
  102. if (empty($conn)) {
  103. return false;
  104. }
  105. try {
  106. $bulk = new \MongoDB\Driver\BulkWrite();
  107. $bulk->delete($whereArr, $options);
  108. $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 30000);
  109. $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
  110. return $result->getDeletedCount();
  111. } catch (\Exception $e) {
  112. CLog::err($e->getMessage()); # 记录错误日志
  113. }
  114. return false;
  115. }
  116. }
  117. /**
  118. * 查询选项构造器
  119. * @author gwang <wanggangzero@qq.com>
  120. */
  121. class MongoQueryOptionBuilder {
  122. /**
  123. * 内部对象
  124. */
  125. private $option = array();
  126. /**
  127. * 返回最终的 options
  128. * @return array
  129. */
  130. public function GetOption() {
  131. return $this->option;
  132. }
  133. /**
  134. * 参照MySQL的 explain
  135. * @param bool $yes
  136. * @return MongoQueryOptionBuilder
  137. */
  138. public function explain($yes = true) {
  139. $this->option[__FUNCTION__] = $yes;
  140. return $this;
  141. }
  142. /**
  143. * 限定返回值数量(0=无限制)
  144. * @param int $limit 默认 0
  145. * @return MongoQueryOptionBuilder
  146. */
  147. public function limit($limit = 0) {
  148. $this->option[__FUNCTION__] = $limit;
  149. return $this;
  150. }
  151. /**
  152. * 限定返回值跳过前n个文档(索引从0开始)
  153. * @param int $n 默认值0
  154. * @return MongoQueryOptionBuilder
  155. */
  156. public function skip($n = 0) {
  157. $this->option[__FUNCTION__] = $n;
  158. return $this;
  159. }
  160. /**
  161. * project
  162. * @param array $arr 假如MQL中为{$project:{key:1,name:"$value.baseInfo.name"}}
  163. * 则此处['key'=>1,'name'=>'$value.baseInfo.name'] 注意如果包含$符使用单引号
  164. * @return MongoQueryOptionBuilder
  165. */
  166. public function projection($arr = array()) {
  167. $this->option[__FUNCTION__] = $arr;
  168. return $this;
  169. }
  170. /**
  171. * 必须与 tailable awaitData 结合使用.
  172. * 设定正整数值可以让服务器阻塞对应的毫秒数(没有数据的前提下,有数据自然直接返回)
  173. * @param int $n
  174. * @return MongoQueryOptionBuilder
  175. */
  176. public function maxAwaitTimeMS($n = 0) {
  177. $this->option[__FUNCTION__] = $n;
  178. return $this;
  179. }
  180. /**
  181. * 只返回索引
  182. * @param bool $yes 默认值false, 当设为true时,你应该了解会返回什么.
  183. * @return $this
  184. */
  185. public function returnKey($yes = false) {
  186. $this->option[__FUNCTION__] = $yes;
  187. return $this;
  188. }
  189. /**
  190. * The sort specification for the ordering of the results.
  191. * Falls back to the deprecated modifier if not specified. "$orderby"
  192. * @param array $arr 假如MQL中为{$sort:{"value.baseInfo.level":-1}则对应["value.baseInfo.level"=>-1]
  193. * @return MongoQueryOptionBuilder
  194. */
  195. public function sort($arr = array()) {
  196. $this->option[__FUNCTION__] = $arr;
  197. return $this;
  198. }
  199. // <editor-fold defaultstate="collapsed" desc="不常用或已经废弃的">
  200. /**
  201. * 不了解, 先不要使用
  202. * @deprecated since version 0
  203. * @param type $yes
  204. * @return $this
  205. */
  206. public function allowDiskUse($yes = true) {
  207. $this->option[__FUNCTION__] = $yes;
  208. return $this;
  209. }
  210. /**
  211. * 不了解, 先不要使用
  212. * @deprecated since version 0
  213. * @param type $yes
  214. * @return $this
  215. */
  216. public function allowPartialResults($yes = true) {
  217. $this->option[__FUNCTION__] = $yes;
  218. return $this;
  219. }
  220. /**
  221. * 不了解, 先不要使用
  222. * @deprecated since version 0
  223. * @param type $yes
  224. * @return $this
  225. */
  226. public function awaitData($yes = true) {
  227. $this->option[__FUNCTION__] = $yes;
  228. return $this;
  229. }
  230. /**
  231. * 返回值第一批的文档数,默认值为101,设置为0表示将建立光标,但不返回任何文档
  232. * @deprecated since version 0 不懂,先不要用
  233. * @param type $size
  234. * @return MongoQueryOptionBuilder
  235. */
  236. public function batchSize($size = 101) {
  237. $this->option[__FUNCTION__] = $size;
  238. return $this;
  239. }
  240. /**
  241. * 语言设置
  242. * @deprecated since version 0 这个太复杂还没研究,先不要用
  243. * @param array|object $collation
  244. * @return MongoQueryOptionBuilder
  245. */
  246. public function collation($collation = array()) {
  247. $this->option[__FUNCTION__] = $collation;
  248. return $this;
  249. }
  250. /**
  251. * 注释
  252. * @param type $msg
  253. * @return MongoQueryOptionBuilder
  254. */
  255. public function comment($msg = "默认注释") {
  256. $this->option[__FUNCTION__] = $msg;
  257. return $this;
  258. }
  259. /**
  260. * 尽最大努力下载数据,(从3.2+已经不再支持)
  261. * @deprecated since version mongodb 3.2+
  262. * @param type $yes
  263. * @return MongoQueryOptionBuilder
  264. */
  265. public function exhaust($yes = true) {
  266. $this->option[__FUNCTION__] = $yes;
  267. return $this;
  268. }
  269. /**
  270. * 不懂, 先不要用
  271. * @deprecated since version 0 在某个index的上限(不包含上限值)
  272. * @param array $arr
  273. * @return MongoQueryOptionBuilder
  274. */
  275. public function max($arr = array()) {
  276. $this->option[__FUNCTION__] = $arr;
  277. return $this;
  278. }
  279. /**
  280. * 不懂,先不要用
  281. * @deprecated since version 0 在某个index的下限(包含下限值)
  282. * @param array $arr
  283. * @return MongoQueryOptionBuilder
  284. */
  285. public function min($arr = array()) {
  286. $this->option[__FUNCTION__] = $arr;
  287. return $this;
  288. }
  289. /**
  290. * @deprecated since version 0 已经废弃
  291. * @param int $i
  292. * @throws \Exception
  293. */
  294. public function maxScan($i = 0) {
  295. throw new \Exception("Deprecated!");
  296. }
  297. /**
  298. * Determines whether to close the cursor after the first batch. Defaults to false.
  299. * @param bool $yes
  300. * @deprecated since version 0 不放心, 先不要用
  301. * @return MongoQueryOptionBuilder
  302. */
  303. public function singleBatch($yes = false) {
  304. $this->option[__FUNCTION__] = $yes;
  305. return $this;
  306. }
  307. /**
  308. * Determines whether to return the record identifier for each document.
  309. * If true, adds a top-level field to the returned documents. "$recordId"
  310. * @deprecated since version 0 这个感觉没啥用,也没测试过,先不要用
  311. * @param bool $yes
  312. * @return $this
  313. */
  314. public function showRecordId($yes = false) {
  315. $this->option[__FUNCTION__] = $yes;
  316. return $this;
  317. }
  318. /**
  319. * Returns a tailable cursor for a capped collection.
  320. * @deprecated since version 0 不懂,先不要用
  321. * @param type $yes
  322. * @return MongoQueryOptionBuilder
  323. */
  324. public function tailable($yes = false) {
  325. $this->option[__FUNCTION__] = $yes;
  326. return $this;
  327. }
  328. // </editor-fold>
  329. }