2
私は、インドに州、郡、村を持つ文書のmongoコレクションを持っています。以下の形式で、私はそのI結果、このような方法で、状態、地区や村でグループにそれをしたい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
では、このような種類の集約が行われました。
どうすればMongoDB
で取得できますか?私はelasticsearch
を使用したくないからです。
これを試して更新しました。 – Hussain