1
각 회사에 도시, 주 (도) 및 국가가 할당되도록 회사 목록이 있다고 가정합니다.국가, 주 및 도시 단위로 집계
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
}
},
...
]
},
...
]
},
...
]