|
25 | 25 | use MongoDB\Builder\BuilderEncoder;
|
26 | 26 | use MongoDB\Builder\Expression\FieldPath;
|
27 | 27 | use MongoDB\Builder\Pipeline;
|
| 28 | +use MongoDB\Builder\Stage; |
| 29 | +use MongoDB\Builder\Stage\CountStage; |
28 | 30 | use MongoDB\Builder\Stage\GroupStage;
|
29 | 31 | use MongoDB\Builder\Stage\LimitStage;
|
30 | 32 | use MongoDB\Builder\Stage\MatchStage;
|
@@ -303,9 +305,12 @@ protected function getPipeline(): array
|
303 | 305 | $columns = [];
|
304 | 306 | }
|
305 | 307 |
|
| 308 | + $pipeline = []; |
306 | 309 | $wheres = $this->compileWheres();
|
307 | 310 |
|
308 |
| - $pipeline = [new MatchStage($wheres)]; |
| 311 | + if (count($wheres)) { |
| 312 | + $pipeline[] = new MatchStage($wheres); |
| 313 | + } |
309 | 314 |
|
310 | 315 | // Use MongoDB's aggregation framework when using grouping or aggregation functions.
|
311 | 316 | if ($this->groups || $this->aggregate) {
|
@@ -369,7 +374,7 @@ protected function getPipeline(): array
|
369 | 374 | // Build the aggregation pipeline.
|
370 | 375 | $pipeline = [];
|
371 | 376 | if ($wheres) {
|
372 |
| - $pipeline[] = new MatchStage(...$wheres); |
| 377 | + $pipeline[] = Stage::match(...$wheres); |
373 | 378 | }
|
374 | 379 |
|
375 | 380 | // apply unwinds for subdocument array aggregation
|
@@ -398,27 +403,14 @@ protected function getPipeline(): array
|
398 | 403 | $pipeline[] = new ProjectStage(...$this->projections);
|
399 | 404 | }
|
400 | 405 |
|
401 |
| - $options = [ |
402 |
| - 'typeMap' => ['root' => 'array', 'document' => 'array'], |
403 |
| - ]; |
404 |
| - |
405 |
| - // Add custom query options |
406 |
| - if (count($this->options)) { |
407 |
| - $options = array_merge($options, $this->options); |
408 |
| - } |
409 |
| - |
410 |
| - $options = $this->inheritConnectionOptions($options); |
411 |
| - |
412 |
| - return ['aggregate' => [$pipeline, $options]]; |
| 406 | + return $pipeline; |
413 | 407 | }
|
414 | 408 |
|
415 | 409 | // Distinct query
|
416 | 410 | if ($this->distinct) {
|
417 | 411 | // Return distinct results directly
|
418 | 412 | $column = $columns[0] ?? '_id';
|
419 | 413 |
|
420 |
| - $options = $this->inheritConnectionOptions(); |
421 |
| - |
422 | 414 | $pipeline[] = new GroupStage(
|
423 | 415 | _id: new FieldPath($column),
|
424 | 416 | _document: new FirstAccumulator(Variable::root()),
|
@@ -562,10 +554,7 @@ public function generateCacheKey()
|
562 | 554 | return md5(serialize(array_values($key)));
|
563 | 555 | }
|
564 | 556 |
|
565 |
| - /** |
566 |
| - * @return self|PipelineBuilder |
567 |
| - * @psalm-return $function === null ? PipelineBuilder : self |
568 |
| - */ |
| 557 | + /** @return ($function === null ? PipelineBuilder : self) */ |
569 | 558 | public function aggregate($function = null, $columns = [])
|
570 | 559 | {
|
571 | 560 | if ($function === null) {
|
@@ -602,6 +591,19 @@ public function aggregate($function = null, $columns = [])
|
602 | 591 | }
|
603 | 592 | }
|
604 | 593 |
|
| 594 | + public function count($columns = '*'): int |
| 595 | + { |
| 596 | + if ($columns !== '*') { |
| 597 | + // @todo trigger warning, $columns is ignored |
| 598 | + } |
| 599 | + |
| 600 | + return $this |
| 601 | + ->aggregate() |
| 602 | + ->count('aggregate') |
| 603 | + ->get() |
| 604 | + ->value('aggregate', 0); |
| 605 | + } |
| 606 | + |
605 | 607 | /** @inheritdoc */
|
606 | 608 | public function exists()
|
607 | 609 | {
|
@@ -980,7 +982,7 @@ public function runPaginationCountQuery($columns = ['*'])
|
980 | 982 | ->toMql();
|
981 | 983 |
|
982 | 984 | // Adds the $count stage to the pipeline
|
983 |
| - $mql['aggregate'][0][] = ['$count' => 'aggregate']; |
| 985 | + $mql['aggregate'][0][] = new CountStage('aggregate'); |
984 | 986 |
|
985 | 987 | return $this->collection->aggregate($mql['aggregate'][0], $mql['aggregate'][1])->toArray();
|
986 | 988 | }
|
|
0 commit comments