按国家,州和城市汇总

时间:2014-07-17 10:49:25

标签: php mongodb mongodb-query aggregation-framework database

假设我们有一个公司列表,以便为每个公司分配城市,州(省)和国家。

我正在寻找解决方案,以already mentioned here创建 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 个答案:

答案 0 :(得分:1)

您需要两个 $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几乎是一般数据结构表示的“通用语言”,不会减损YAML或简化的XML或其他。将这些东西翻译成母语表示并不难,尤其是当存在许多图书馆时。