-2

How can I resolve following error on Yii collection aggregate:

“each item in the pipeline must be a document”

with this code:

$collection = Yii::$app->mongodb->getCollection('lc_executive_allocate');
$asCursor = true;
$result = $collection->aggregate([
    [
        '$match' => [
            'stage' => '2',
            'completed' => '1',
            'status' => '1',
            'assignedOn' => ['$gte' => '2018-03-01', '$lte' => '2018-03-29'],
        ],
    ],
    [
        '$group' => [
            '_id' => ['userId' => '$userId', 'count' => ['$sum' => '1']],
        ],
    ],
    ['$sort' => ['count' => '-1']],
    ['$limit' => '1'],
    $asCursor,
]);
rob006
  • 21,383
  • 5
  • 53
  • 74
  • 2
    Welcome to StackOverflow! Please read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). This will make it much easier for others to help you. – Aditi Apr 03 '18 at 06:52

1 Answers1

0

Try

$result = $collection->aggregate(
    [
        [
            '$match' => [
                'stage' => '2',
                'completed' => '1',
                'status' => '1',
                'assignedOn' => ['$gte' => '2018-03-01', '$lte' => '2018-03-29'],
            ],
        ],
        [
            '$group' => [
                '_id' => ['userId' => '$userId', 'count' => ['$sum' => '1']],
            ],
        ],
        ['$sort' => ['count' => '-1']],
        ['$limit' => '1'],
    ],
    ['cursor' => true] // <- this as second argument of `aggregate()`
);

In case 'cursor' option is specified MongoDB\Driver\Cursor instance is returned, otherwise - an array of aggregation results.

https://www.yiiframework.com/extension/yiisoft/yii2-mongodb/doc/api/2.1/yii-mongodb-collection#aggregate()-detail

rob006
  • 21,383
  • 5
  • 53
  • 74