2017-04-08 307 views
2

我有印度的州,地區和村莊的文件集合。Mongodb中聚合的聚合

{ 
    "_id" : ObjectId("58db5a5f8f7b5013ad477564"), 
    "district" : "Chittor", 
    "state" : "Andhra Pradesh", 
    "village" : "Chittoor" 
}, 
{ 
    "_id" : ObjectId("58db5a628f7b5013ad477566"), 
    "district" : "Dibrugarh", 
    "state" : "Assam", 
    "village" : "Dibrugarh" 
}, 
{ 
    "_id" : ObjectId("58db5a628f7b5013ad47756d"), 
    "district" : "Lakhimpur", 
    "state" : "Assam", 
    "village" : "Kadam" 
}, 
{ 
    "_id" : ObjectId("58db5a628f7b5013ad47756e"), 
    "district" : "Sonitpur", 
    "state" : "Assam", 
    "village" : "Helem" 
}, 
{ 
    "_id" : ObjectId("58db5a628f7b5013ad477572"), 
    "district" : "Gandhinagar", 
    "state" : "Gujarat", 
    "village" : "Mansa" 
}, 
{ 
    "_id" : ObjectId("58db5a628f7b5013ad477573"), 
    "district" : "Gandhinagar", 
    "state" : "Gujarat", 
    "village" : "Kalol" 
}, 
{ 
    "_id" : ObjectId("58db5a628f7b5013ad477574"), 
    "district" : "Ahmadabad", 
    "state" : "Gujarat", 
    "village" : "Barwala" 
} 

我要通過國家,地區和村民小組就以這樣的方式,我的結果在下面的格式

[ 
    { 
    "state": "Gujarat", 
    "districts": [ 
     { 
     "district": "Gandhinagar", 
     "villages": [ 
      { 
      "village": "Mansa" 
      }, 
      { 
      "village": "Kalol" 
      } 
     ] 
     }, 
     { 
     "district": "Ahmadabad", 
     "villages": [ 
      { 
      "village": "Barwala" 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    "state": "Assam", 
    "districts": [ 
     { 
     "district": "Sonitpur", 
     "villages": [ 
      { 
      "village": "Helem" 
      } 
     ] 
     }, 
     { 
     "district": "Lakhimpur", 
     "villages": [ 
      { 
      "village": "Kadam" 
      } 
     ] 
     }, 
     { 
     "district": "Dibrugarh", 
     "villages": [ 
      { 
      "village": "Dibrugarh" 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    "state": "Andhra Pradesh", 
    "districts": [ 
     { 
     "district": "Chittor", 
     "villages": [ 
      { 
      "village": "Chittor" 
      } 
     ] 
     } 
    ] 
    } 
] 

我試過,但沒有找到一種方式來獲得這樣的聚集。

1) db.historical_prices.aggregate({$group: {_id: {"state": "$state"}}}, {$group: {_id: {"commodity": "$commodity"}}}) 

2) db.places.aggregate() 
     .match(qb.where("enabled").eq(false)) 
     .group({ _id: {"state": "$state", "district": "$district"}, "districts": {$push: {district: "$district"}}, }) 

我在elasticsearch中制定了這樣的聚合。

GET /places/_search 
{ 
    "query": { "match_all": {} }, 
    "size": 0, 
    "aggs": { 
    "group_by_state": { 
     "terms": { 
     "field": "state.keyword" 
     }, 
     "aggs": { 
     "group_by_district": { 
      "terms": { 
      "field": "district.keyword" 
      }, 
      "aggs": { 
      "group_by_village": { 
       "terms": { 
       "field": "village.keyword" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

我怎樣才能得到這個MongoDB?因爲我不想僅使用elasticsearch來做這件事。

+0

嘗試更新它。 – Hussain

回答

2

您可以嘗試下面的聚合。下面的查詢$groupstatedistrict上積累villages後面$groupstate積累district和以前收集的villages

db.collection.aggregate([{ 
    $group: { 
     _id: {state:"$state", district:"$district"}, 
     "villages":{$addToSet:{village:"$village"}} 
    } 
},{ 
    $group: { 
     _id: {state:"$_id.state"}, 
     "districts":{$push:{villages:"$villages", district:"$_id.district"}} 
    } 
}])