php - 按国家、州和城市汇总

标签 php mongodb mongodb-query aggregation-framework database

假设我们有一个公司列表,每个公司都被分配了城市、州(省)和国家/地区。

我正在寻找一个解决方案,创建一个3 级聚合 作为already mentioned here .不幸的是,我无法将原始 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
                        }
                    },

                    ...

                ]
            },

            ...

          ]
    },

    ...

]

最佳答案

你想要两个 $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 或其他。将这些东西翻译成本地语言表示确实不难,尤其是当存在许多库可以这样做时。

关于php - 按国家、州和城市汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24801562/

相关文章:

node.js - 在 mongoose 中使用 mongodb 多键索引方法索引引用数组

php - 如何在MySql数据库中存储简历详细信息

php - 为什么 UTF-8 在我的一个 PHP 文件中有效,而在另一个文件中无效?

mongodb - 所有者 1 的 NoSQL 设计 :M PC and PC 1:M Parts

Spring Data mongodb聚合匹配

python - Mongo 嵌入式文档查询

mongodb - 重命名/更新所有文档中的特定标签

php - .rar, .zip 文件 MIME 类型

php - Cakephp 3 给我 fatal error : Uncaught Error: Class 'Cake\Http\Server' not found

arrays - mongoDB中数组中的字符串字段值长度