按国家、州和城市进行聚合

3
假设我们有一个公司列表,每个公司都被分配了城市、州(省)和国家。
我正在寻找一种解决方案,创建一个3级聚合,就像在这里已经提到的。不幸的是,我无法将原始的MongoDB查询翻译成PHP。
当前异常:
exception: invalid operator '$push'

数据库项目:

{
    "_id" : ObjectId("52c85fafc8526a4d0d21e0be"),
    "city" : "Freiburg",
    "country" : "DE",
    "state" : "DE-BW",
    "_coords" : {
        "latitude" : 47.9990077,
        "longitude" : 7.842104299999999
    }
}

来源:

$collection = $this->getClient()->getCollection('companies');

$ops = array(
    array(
    '$group' => array(
        '_id' => array(
        'country' => '$country',
        'state' => '$state', 
        'city' => '$city' 
        ),
    ),
    ),
    array(
    '$group' => array(
        '_id' => array(
        'country' => '$_id.country',
        'state' => '$_id.state'
        ),
    ),
    ),
    array(
    '$group' => array(
        '_id' => array(
        'country' => '$_id.country',
        'state' => array('$push' => '$_id.state')
        ),
    ),
    ),
    array(
    '$sort' => array(
        'state' => 1
    ),
    )
);

$results = $collection->aggregate($ops);

预期结果:

[
    {
    "country" : "DE",
        "states" :  
        [     
            {
                "state" : "DE-BW",
                "cities" : [
                    {
                        "city": "Freiburg",
                        "_coords": {
                            "latitude" : 47.9990077,
                            "longitude" : 7.842104299999999
                        }
                    },

                    ...

                ]
            },

            ...

          ]
    },

    ...

]
1个回答

4
你想在这里有两个$group级别。可选地使用$addToSet而不是$push来确保唯一性:
对于其他人的基本JSON表示法:
db.country.aggregate([
    { "$group": { 
        "_id": {
            "country": "$country",
            "state": "$state"
        },
        "cities": { 
            "$addToSet": {
                "city": "$city",
                "_coords": "$_coords"
            }
        }
    }},
    { "$group": {
        "_id": "$_id.country",
        "states": {
            "$addToSet": {
                "state": "$_id.state",
                "cities": "$cities"
            }
        }
    }}       
])

对于您来说,PHP表示为:

$collection->aggregate(
    array(
        array(
            '$group' => array(
                 '_id' => array(
                     'country' => 'country',
                     'state' => '$state'
                 ),
                 '$cities' => array(
                     '$addToSet' => array(
                         'city' => '$city',
                         '_coords' => '$_coords'
                      )
                 )
            )
        ),
        array(
            '$group' => array(
                '_id' => '$_id.country',
                'states' => array(
                    '$addToSet' => array(
                         'state' => '$_id.state',
                         'cities' => '$cities'                                  
                     )              
                )              
            )
        )
    )
);

但是,认真学习如何使用json_decode。JSON基本上是通用数据结构表示的“lingua franca”,并不会削弱YAML或简化的XML或其他语言的重要性。将这些东西转换为本地语言表示其实并不难,特别是存在许多库可以完成此操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接