如何在聚合mongodb中进行匹配组?

时间:2017-05-06 10:57:11

标签: mongodb yii2

如果我写匹配和组,就没有用。如果我删除了组或匹配它可以正常工作

$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
                )
              )
            );

1 个答案:

答案 0 :(得分:0)

我解决了它,当我在小组之前放置比赛并且它对我有效时它起作用。

$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
                )
              )
            );