K
K
Khurshed Abdujalil2017-05-06 13:21:39
Yii
Khurshed Abdujalil, 2017-05-06 13:21:39

How to make group and match selection in mongodb on yii2?

When I make a match selection with group, the result is empty, when I remove group, the records are displayed.

$collection = Yii::$app->mongodb->getCollection('products');
        $result = $collection->aggregate(array(
            array(
              '$group' => array(
                '_id' => array('city_id' => '$city_id'),
                'count' => array( '$sum' => 1)
              ),
            ),
            array(
              '$match' => array('user_id' => $filter['user_id']),
            ),
            array(
              '$limit' => 20
            )
          )
        );

What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Khurshed Abdujalil, 2017-05-08
@akhur

put $match before $group and selection worked

$collection = Yii::$app->mongodb->getCollection('products');
        $result = $collection->aggregate(array(
            array(
              '$match' => array('user_id' => $filter['user_id']),
            ),
            array(
              '$group' => array(
                '_id' => array('city_id' => '$city_id'),
                'count' => array( '$sum' => 1)
              ),
            ),
            array(
              '$limit' => 20
            )
          )
        );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question