假设我们有一个公司列表,每个公司都被分配了城市、州(省)和国家/地区。
我正在寻找一个解决方案,创建一个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/