Answer the question
In order to leave comments, you need to log in
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
)
)
);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question