You are here

public function TopController::group in MongoDB 8.2

Command wrapper for removed MongoDB group() method/command.

Parameters

\MongoDB\Collection $collection: The collection on which to perform the command.

string $key: The grouping key.

array $cond: The condition.

Return value

array An array of stdClass rows with the following properties:

  • _id: the URL
  • count: the number of occurrences.

It may be empty.

Throws

\MongoDB\Driver\Exception\RuntimeException

\MongoDB\Exception\InvalidArgumentException

\MongoDB\Exception\UnexpectedValueException

\MongoDB\Exception\UnsupportedException

1 call to TopController::group()
TopController::getRowData in modules/mongodb_watchdog/src/Controller/TopController.php
Obtain the data from the logger.

File

modules/mongodb_watchdog/src/Controller/TopController.php, line 218

Class

TopController
The Top403/Top404 controllers.

Namespace

Drupal\mongodb_watchdog\Controller

Code

public function group(Collection $collection, string $key, array $cond) : array {
  $pipeline = [];
  if (!empty($cond)) {
    $pipeline[] = [
      '$match' => $cond,
    ];
  }
  if (!empty($key)) {
    $pipeline[] = [
      '$group' => [
        '_id' => "\${$key}",
        'count' => [
          '$sum' => 1,
        ],
      ],
    ];
  }
  $pipeline[] = [
    '$sort' => [
      'count' => -1,
      '_id' => 1,
    ],
  ];

  // Aggregate always returns a cursor since MongoDB 3.6.

  /** @var \MongoDB\Driver\CursorInterface $res */
  $res = $collection
    ->aggregate($pipeline);
  $res
    ->setTypeMap(static::TYPE_MAP);
  $ret = $res
    ->toArray();
  return $ret;
}