Query.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\mongodb;
  8. use yii\base\Component;
  9. use yii\db\QueryInterface;
  10. use yii\db\QueryTrait;
  11. use Yii;
  12. use yii\helpers\ArrayHelper;
  13. /**
  14. * Query represents Mongo "find" operation.
  15. *
  16. * Query provides a set of methods to facilitate the specification of "find" command.
  17. * These methods can be chained together.
  18. *
  19. * For example,
  20. *
  21. * ```php
  22. * $query = new Query();
  23. * // compose the query
  24. * $query->select(['name', 'status'])
  25. * ->from('customer')
  26. * ->limit(10);
  27. * // execute the query
  28. * $rows = $query->all();
  29. * ```
  30. *
  31. * @property Collection $collection Collection instance. This property is read-only.
  32. *
  33. * @author Paul Klimov <klimov.paul@gmail.com>
  34. * @since 2.0
  35. */
  36. class Query extends Component implements QueryInterface
  37. {
  38. use QueryTrait;
  39. /**
  40. * @var array the fields of the results to return. For example: `['name', 'group_id']`, `['name' => true, '_id' => false]`.
  41. * Unless directly excluded, the "_id" field is always returned. If not set, it means selecting all columns.
  42. * @see select()
  43. */
  44. public $select = [];
  45. /**
  46. * @var string|array the collection to be selected from. If string considered as the name of the collection
  47. * inside the default database. If array - first element considered as the name of the database,
  48. * second - as name of collection inside that database
  49. * @see from()
  50. */
  51. public $from;
  52. /**
  53. * @var array cursor options in format: optionKey => optionValue
  54. * @see \MongoDB\Driver\Cursor::addOption()
  55. * @see options()
  56. */
  57. public $options = [];
  58. /**
  59. * Returns the Mongo collection for this query.
  60. * @param Connection $db Mongo connection.
  61. * @return Collection collection instance.
  62. */
  63. public function getCollection($db = null)
  64. {
  65. if ($db === null) {
  66. $db = Yii::$app->get('mongodb');
  67. }
  68. return $db->getCollection($this->from);
  69. }
  70. /**
  71. * Sets the list of fields of the results to return.
  72. * @param array $fields fields of the results to return.
  73. * @return $this the query object itself.
  74. */
  75. public function select(array $fields)
  76. {
  77. $this->select = $fields;
  78. return $this;
  79. }
  80. /**
  81. * Sets the collection to be selected from.
  82. * @param string|array the collection to be selected from. If string considered as the name of the collection
  83. * inside the default database. If array - first element considered as the name of the database,
  84. * second - as name of collection inside that database
  85. * @return $this the query object itself.
  86. */
  87. public function from($collection)
  88. {
  89. $this->from = $collection;
  90. return $this;
  91. }
  92. /**
  93. * Sets the cursor options.
  94. * @param array $options cursor options in format: optionName => optionValue
  95. * @return $this the query object itself
  96. * @see addOptions()
  97. */
  98. public function options($options)
  99. {
  100. $this->options = $options;
  101. return $this;
  102. }
  103. /**
  104. * Adds additional cursor options.
  105. * @param array $options cursor options in format: optionName => optionValue
  106. * @return $this the query object itself
  107. * @see options()
  108. */
  109. public function addOptions($options)
  110. {
  111. if (is_array($this->options)) {
  112. $this->options = array_merge($this->options, $options);
  113. } else {
  114. $this->options = $options;
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Helper method for easy querying on values containing some common operators.
  120. *
  121. * The comparison operator is intelligently determined based on the first few characters in the given value and
  122. * internally translated to a MongoDB operator.
  123. * In particular, it recognizes the following operators if they appear as the leading characters in the given value:
  124. * <: the column must be less than the given value ($lt).
  125. * >: the column must be greater than the given value ($gt).
  126. * <=: the column must be less than or equal to the given value ($lte).
  127. * >=: the column must be greater than or equal to the given value ($gte).
  128. * <>: the column must not be the same as the given value ($ne). Note that when $partialMatch is true, this would mean the value must not be a substring of the column.
  129. * =: the column must be equal to the given value ($eq).
  130. * none of the above: use the $defaultOperator
  131. *
  132. * Note that when the value is empty, no comparison expression will be added to the search condition.
  133. *
  134. * @param string $name column name
  135. * @param string $value column value
  136. * @param string $defaultOperator Defaults to =, performing an exact match.
  137. * For example: use 'LIKE' or 'REGEX' for partial cq regex matching
  138. * @see Collection::buildCondition()
  139. * @return $this the query object itself.
  140. * @since 2.0.5
  141. */
  142. public function andFilterCompare($name, $value, $defaultOperator = '=')
  143. {
  144. $matches = [];
  145. if (preg_match('/^(<>|>=|>|<=|<|=)/', $value, $matches)) {
  146. $op = $matches[1];
  147. $value = substr($value, strlen($op));
  148. } else {
  149. $op = $defaultOperator;
  150. }
  151. return $this->andFilterWhere([$op, $name, $value]);
  152. }
  153. /**
  154. * Prepares for query building.
  155. * This method is called before actual query composition, e.g. building cursor, count etc.
  156. * You may override this method to do some final preparation work before query execution.
  157. * @return $this a prepared query instance.
  158. * @since 2.1.3
  159. */
  160. public function prepare()
  161. {
  162. return $this;
  163. }
  164. /**
  165. * Builds the MongoDB cursor for this query.
  166. * @param Connection $db the MongoDB connection used to execute the query.
  167. * @return \MongoDB\Driver\Cursor mongo cursor instance.
  168. */
  169. public function buildCursor($db = null)
  170. {
  171. $this->prepare();
  172. $options = $this->options;
  173. if (!empty($this->orderBy)) {
  174. $options['sort'] = $this->orderBy;
  175. }
  176. $options['limit'] = $this->limit;
  177. $options['skip'] = $this->offset;
  178. $cursor = $this->getCollection($db)->find($this->composeCondition(), $this->select, $options);
  179. return $cursor;
  180. }
  181. /**
  182. * Fetches rows from the given Mongo cursor.
  183. * @param \MongoDB\Driver\Cursor $cursor Mongo cursor instance to fetch data from.
  184. * @param bool $all whether to fetch all rows or only first one.
  185. * @param string|callable $indexBy the column name or PHP callback,
  186. * by which the query results should be indexed by.
  187. * @throws Exception on failure.
  188. * @return array|bool result.
  189. */
  190. protected function fetchRows($cursor, $all = true, $indexBy = null)
  191. {
  192. $token = 'fetch cursor id = ' . $cursor->getId();
  193. Yii::info($token, __METHOD__);
  194. try {
  195. Yii::beginProfile($token, __METHOD__);
  196. $result = $this->fetchRowsInternal($cursor, $all);
  197. Yii::endProfile($token, __METHOD__);
  198. return $result;
  199. } catch (\Exception $e) {
  200. Yii::endProfile($token, __METHOD__);
  201. throw new Exception($e->getMessage(), (int) $e->getCode(), $e);
  202. }
  203. }
  204. /**
  205. * @param \MongoDB\Driver\Cursor $cursor Mongo cursor instance to fetch data from.
  206. * @param bool $all whether to fetch all rows or only first one.
  207. * @return array|bool result.
  208. * @see Query::fetchRows()
  209. */
  210. protected function fetchRowsInternal($cursor, $all)
  211. {
  212. $result = [];
  213. if ($all) {
  214. foreach ($cursor as $row) {
  215. $result[] = $row;
  216. }
  217. } else {
  218. if ($row = current($cursor->toArray())) {
  219. $result = $row;
  220. } else {
  221. $result = false;
  222. }
  223. }
  224. return $result;
  225. }
  226. /**
  227. * Starts a batch query.
  228. *
  229. * A batch query supports fetching data in batches, which can keep the memory usage under a limit.
  230. * This method will return a [[BatchQueryResult]] object which implements the `Iterator` interface
  231. * and can be traversed to retrieve the data in batches.
  232. *
  233. * For example,
  234. *
  235. * ```php
  236. * $query = (new Query)->from('user');
  237. * foreach ($query->batch() as $rows) {
  238. * // $rows is an array of 10 or fewer rows from user collection
  239. * }
  240. * ```
  241. *
  242. * @param int $batchSize the number of records to be fetched in each batch.
  243. * @param Connection $db the MongoDB connection. If not set, the "mongodb" application component will be used.
  244. * @return BatchQueryResult the batch query result. It implements the `Iterator` interface
  245. * and can be traversed to retrieve the data in batches.
  246. * @since 2.1
  247. */
  248. public function batch($batchSize = 100, $db = null)
  249. {
  250. return Yii::createObject([
  251. 'class' => BatchQueryResult::className(),
  252. 'query' => $this,
  253. 'batchSize' => $batchSize,
  254. 'db' => $db,
  255. 'each' => false,
  256. ]);
  257. }
  258. /**
  259. * Starts a batch query and retrieves data row by row.
  260. * This method is similar to [[batch()]] except that in each iteration of the result,
  261. * only one row of data is returned. For example,
  262. *
  263. * ```php
  264. * $query = (new Query)->from('user');
  265. * foreach ($query->each() as $row) {
  266. * }
  267. * ```
  268. *
  269. * @param int $batchSize the number of records to be fetched in each batch.
  270. * @param Connection $db the MongoDB connection. If not set, the "mongodb" application component will be used.
  271. * @return BatchQueryResult the batch query result. It implements the `Iterator` interface
  272. * and can be traversed to retrieve the data in batches.
  273. * @since 2.1
  274. */
  275. public function each($batchSize = 100, $db = null)
  276. {
  277. return Yii::createObject([
  278. 'class' => BatchQueryResult::className(),
  279. 'query' => $this,
  280. 'batchSize' => $batchSize,
  281. 'db' => $db,
  282. 'each' => true,
  283. ]);
  284. }
  285. /**
  286. * Executes the query and returns all results as an array.
  287. * @param Connection $db the Mongo connection used to execute the query.
  288. * If this parameter is not given, the `mongodb` application component will be used.
  289. * @return array the query results. If the query results in nothing, an empty array will be returned.
  290. */
  291. public function all($db = null)
  292. {
  293. if (!empty($this->emulateExecution)) {
  294. return [];
  295. }
  296. $cursor = $this->buildCursor($db);
  297. $rows = $this->fetchRows($cursor, true, $this->indexBy);
  298. return $this->populate($rows);
  299. }
  300. /**
  301. * Converts the raw query results into the format as specified by this query.
  302. * This method is internally used to convert the data fetched from database
  303. * into the format as required by this query.
  304. * @param array $rows the raw query result from database
  305. * @return array the converted query result
  306. */
  307. public function populate($rows)
  308. {
  309. if ($this->indexBy === null) {
  310. return $rows;
  311. }
  312. $result = [];
  313. foreach ($rows as $row) {
  314. $result[ArrayHelper::getValue($row, $this->indexBy)] = $row;
  315. }
  316. return $result;
  317. }
  318. /**
  319. * Executes the query and returns a single row of result.
  320. * @param Connection $db the Mongo connection used to execute the query.
  321. * If this parameter is not given, the `mongodb` application component will be used.
  322. * @return array|false the first row (in terms of an array) of the query result. `false` is returned if the query
  323. * results in nothing.
  324. */
  325. public function one($db = null)
  326. {
  327. if (!empty($this->emulateExecution)) {
  328. return false;
  329. }
  330. $cursor = $this->buildCursor($db);
  331. return $this->fetchRows($cursor, false);
  332. }
  333. /**
  334. * Returns the query result as a scalar value.
  335. * The value returned will be the first column in the first row of the query results.
  336. * Column `_id` will be automatically excluded from select fields, if [[select]] is not empty and
  337. * `_id` is not selected explicitly.
  338. * @param Connection $db the MongoDB connection used to generate the query.
  339. * If this parameter is not given, the `mongodb` application component will be used.
  340. * @return string|null|false the value of the first column in the first row of the query result.
  341. * `false` is returned if the query result is empty.
  342. * @since 2.1.2
  343. */
  344. public function scalar($db = null)
  345. {
  346. if (!empty($this->emulateExecution)) {
  347. return null;
  348. }
  349. $originSelect = (array)$this->select;
  350. if (!isset($originSelect['_id']) && array_search('_id', $originSelect, true) === false) {
  351. $this->select['_id'] = false;
  352. }
  353. $cursor = $this->buildCursor($db);
  354. $row = $this->fetchRows($cursor, false);
  355. if (empty($row)) {
  356. return false;
  357. }
  358. return reset($row);
  359. }
  360. /**
  361. * Executes the query and returns the first column of the result.
  362. * Column `_id` will be automatically excluded from select fields, if [[select]] is not empty and
  363. * `_id` is not selected explicitly.
  364. * @param Connection $db the MongoDB connection used to generate the query.
  365. * If this parameter is not given, the `mongodb` application component will be used.
  366. * @return array the first column of the query result. An empty array is returned if the query results in nothing.
  367. * @since 2.1.2
  368. */
  369. public function column($db = null)
  370. {
  371. if (!empty($this->emulateExecution)) {
  372. return [];
  373. }
  374. $originSelect = (array)$this->select;
  375. if (!isset($originSelect['_id']) && array_search('_id', $originSelect, true) === false) {
  376. $this->select['_id'] = false;
  377. }
  378. if (is_string($this->indexBy) && $originSelect && count($originSelect) === 1) {
  379. $this->select[] = $this->indexBy;
  380. }
  381. $cursor = $this->buildCursor($db);
  382. $rows = $this->fetchRows($cursor, true);
  383. if (empty($rows)) {
  384. return [];
  385. }
  386. $results = [];
  387. foreach ($rows as $row) {
  388. $value = reset($row);
  389. if ($this->indexBy === null) {
  390. $results[] = $value;
  391. } else {
  392. if ($this->indexBy instanceof \Closure) {
  393. $results[call_user_func($this->indexBy, $row)] = $value;
  394. } else {
  395. $results[$row[$this->indexBy]] = $value;
  396. }
  397. }
  398. }
  399. return $results;
  400. }
  401. /**
  402. * Performs 'findAndModify' query and returns a single row of result.
  403. * @param array $update update criteria
  404. * @param array $options list of options in format: optionName => optionValue.
  405. * @param Connection $db the Mongo connection used to execute the query.
  406. * @return array|null the original document, or the modified document when $options['new'] is set.
  407. */
  408. public function modify($update, $options = [], $db = null)
  409. {
  410. if (!empty($this->emulateExecution)) {
  411. return null;
  412. }
  413. $this->prepare();
  414. $collection = $this->getCollection($db);
  415. if (!empty($this->orderBy)) {
  416. $options['sort'] = $this->orderBy;
  417. }
  418. $options['fields'] = $this->select;
  419. return $collection->findAndModify($this->composeCondition(), $update, $options);
  420. }
  421. /**
  422. * Returns the number of records.
  423. * @param string $q kept to match [[QueryInterface]], its value is ignored.
  424. * @param Connection $db the Mongo connection used to execute the query.
  425. * If this parameter is not given, the `mongodb` application component will be used.
  426. * @return int number of records
  427. * @throws Exception on failure.
  428. */
  429. public function count($q = '*', $db = null)
  430. {
  431. if (!empty($this->emulateExecution)) {
  432. return 0;
  433. }
  434. $this->prepare();
  435. $collection = $this->getCollection($db);
  436. return $collection->count($this->where, $this->options);
  437. }
  438. /**
  439. * Returns a value indicating whether the query result contains any row of data.
  440. * @param Connection $db the Mongo connection used to execute the query.
  441. * If this parameter is not given, the `mongodb` application component will be used.
  442. * @return bool whether the query result contains any row of data.
  443. */
  444. public function exists($db = null)
  445. {
  446. if (!empty($this->emulateExecution)) {
  447. return false;
  448. }
  449. $cursor = $this->buildCursor($db);
  450. foreach ($cursor as $row) {
  451. return true;
  452. }
  453. return false;
  454. }
  455. /**
  456. * Returns the sum of the specified column values.
  457. * @param string $q the column name.
  458. * Make sure you properly quote column names in the expression.
  459. * @param Connection $db the Mongo connection used to execute the query.
  460. * If this parameter is not given, the `mongodb` application component will be used.
  461. * @return int the sum of the specified column values
  462. */
  463. public function sum($q, $db = null)
  464. {
  465. if (!empty($this->emulateExecution)) {
  466. return 0;
  467. }
  468. return $this->aggregate($q, 'sum', $db);
  469. }
  470. /**
  471. * Returns the average of the specified column values.
  472. * @param string $q the column name.
  473. * Make sure you properly quote column names in the expression.
  474. * @param Connection $db the Mongo connection used to execute the query.
  475. * If this parameter is not given, the `mongodb` application component will be used.
  476. * @return int the average of the specified column values.
  477. */
  478. public function average($q, $db = null)
  479. {
  480. if (!empty($this->emulateExecution)) {
  481. return 0;
  482. }
  483. return $this->aggregate($q, 'avg', $db);
  484. }
  485. /**
  486. * Returns the minimum of the specified column values.
  487. * @param string $q the column name.
  488. * Make sure you properly quote column names in the expression.
  489. * @param Connection $db the MongoDB connection used to execute the query.
  490. * If this parameter is not given, the `db` application component will be used.
  491. * @return int the minimum of the specified column values.
  492. */
  493. public function min($q, $db = null)
  494. {
  495. return $this->aggregate($q, 'min', $db);
  496. }
  497. /**
  498. * Returns the maximum of the specified column values.
  499. * @param string $q the column name.
  500. * Make sure you properly quote column names in the expression.
  501. * @param Connection $db the MongoDB connection used to execute the query.
  502. * If this parameter is not given, the `mongodb` application component will be used.
  503. * @return int the maximum of the specified column values.
  504. */
  505. public function max($q, $db = null)
  506. {
  507. return $this->aggregate($q, 'max', $db);
  508. }
  509. /**
  510. * Performs the aggregation for the given column.
  511. * @param string $column column name.
  512. * @param string $operator aggregation operator.
  513. * @param Connection $db the database connection used to execute the query.
  514. * @return int aggregation result.
  515. */
  516. protected function aggregate($column, $operator, $db)
  517. {
  518. if (!empty($this->emulateExecution)) {
  519. return null;
  520. }
  521. $this->prepare();
  522. $collection = $this->getCollection($db);
  523. $pipelines = [];
  524. if ($this->where !== null) {
  525. $pipelines[] = ['$match' => $this->where];
  526. }
  527. $pipelines[] = [
  528. '$group' => [
  529. '_id' => '1',
  530. 'total' => [
  531. '$' . $operator => '$' . $column
  532. ],
  533. ]
  534. ];
  535. $result = $collection->aggregate($pipelines);
  536. if (array_key_exists(0, $result)) {
  537. return $result[0]['total'];
  538. }
  539. return null;
  540. }
  541. /**
  542. * Returns a list of distinct values for the given column across a collection.
  543. * @param string $q column to use.
  544. * @param Connection $db the MongoDB connection used to execute the query.
  545. * If this parameter is not given, the `mongodb` application component will be used.
  546. * @return array array of distinct values
  547. */
  548. public function distinct($q, $db = null)
  549. {
  550. if (!empty($this->emulateExecution)) {
  551. return [];
  552. }
  553. $this->prepare();
  554. $collection = $this->getCollection($db);
  555. if ($this->where !== null) {
  556. $condition = $this->where;
  557. } else {
  558. $condition = [];
  559. }
  560. $result = $collection->distinct($q, $condition);
  561. if ($result === false) {
  562. return [];
  563. }
  564. return $result;
  565. }
  566. /**
  567. * Composes condition from raw [[where]] value.
  568. * @return array conditions.
  569. */
  570. private function composeCondition()
  571. {
  572. if ($this->where === null) {
  573. return [];
  574. }
  575. return $this->where;
  576. }
  577. }