Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按国家、州和城市划分的合计_Php_Mongodb_Mongodb Query_Aggregation Framework_Database - Fatal编程技术网

Php 按国家、州和城市划分的合计

Php 按国家、州和城市划分的合计,php,mongodb,mongodb-query,aggregation-framework,database,Php,Mongodb,Mongodb Query,Aggregation Framework,Database,假设我们有一个公司列表,每个公司被分配到城市、州(省)和国家 我正在寻找一个解决方案,创建一个3级聚合。不幸的是,我无法将原始MongoDB查询转换为PHP 当前例外情况: exception: invalid operator '$push' 数据库项: { "_id" : ObjectId("52c85fafc8526a4d0d21e0be"), "city" : "Freiburg", "country" : "DE", "state" : "DE-BW"

假设我们有一个公司列表,每个公司被分配到城市、州(省)和国家

我正在寻找一个解决方案,创建一个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
                        }
                    },

                    ...

                ]
            },

            ...

          ]
    },

    ...

]

您需要两个级别。可选地使用而非唯一性:

对于其他人,使用基本JSON符号:

db.country.aggregate([
{“$group”:{
“_id”:{
“国家”:“$country”,
“状态”:“$state”
},
“城市”:
“$addToSet”:{
“城市”:“$city”,
“\u coords”:“$\u coords”
}
}
}},
{“$组”:{
“\u id”:“$\u id.country”,
“国家”:{
“$addToSet”:{
“州”:“$\u id.state”,
“城市”:“$城市”
}
}
}}       
])
用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\u解码
。JSON几乎是通用数据结构表示的“通用语言”,而不是YAML、简化XML或其他语言。把这些东西翻译成母语表达并不难,尤其是当有很多天秤座的人这样做的时候