mongodb多节点聚合分组查询

阅读更多
1、区域元数据JSON格式:

{
    "_id" : ObjectId("5962e1cd0163d9cfb795bc76"),
    "id" : "25",
    "text" : "湖南",
    "children" : [ 
        {
            "id" : "0730",
            "text" : "岳阳",
            "children" : [ 
                {
                    "id" : "073003",
                    "text" : "华容",
                    "children" : null
                }, 
                {
                    "id" : "073004",
                    "text" : "临湘",
                    "children" : null
                }, 
                {
                    "id" : "073005",
                    "text" : "平江",
                    "children" : null
                }, 
                {
                    "id" : "073006",
                    "text" : "汨罗",
                    "children" : null
                }, 
                {
                    "id" : "073007",
                    "text" : "云溪",
                    "children" : null
                }, 
                {
                    "id" : "073008",
                    "text" : "君山",
                    "children" : null
                }, 
                {
                    "id" : "073009",
                    "text" : "屈原",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0731",
            "text" : "长沙",
            "children" : [ 
                {
                    "id" : "073100",
                    "text" : "长沙市区",
                    "children" : null
                }, 
                {
                    "id" : "073101",
                    "text" : "长沙县",
                    "children" : null
                }, 
                {
                    "id" : "073102",
                    "text" : "宁乡县",
                    "children" : null
                }, 
                {
                    "id" : "073103",
                    "text" : "望城县",
                    "children" : null
                }, 
                {
                    "id" : "073104",
                    "text" : "捞刀河镇",
                    "children" : null
                }, 
                {
                    "id" : "073105",
                    "text" : "雷锋镇",
                    "children" : null
                }, 
                {
                    "id" : "073106",
                    "text" : "雨敞坪镇",
                    "children" : null
                }, 
                {
                    "id" : "073107",
                    "text" : "坪塘乡",
                    "children" : null
                }, 
                {
                    "id" : "073108",
                    "text" : "桥驿镇",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0732",
            "text" : "湘潭",
            "children" : [ 
                {
                    "id" : "073201",
                    "text" : "韶山",
                    "children" : null
                }, 
                {
                    "id" : "073202",
                    "text" : "湘乡",
                    "children" : null
                }, 
                {
                    "id" : "073203",
                    "text" : "湘潭县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0733",
            "text" : "株洲",
            "children" : [ 
                {
                    "id" : "073300",
                    "text" : "株洲市",
                    "children" : null
                }, 
                {
                    "id" : "073301",
                    "text" : "醴陵市",
                    "children" : null
                }, 
                {
                    "id" : "073302",
                    "text" : "株洲县",
                    "children" : null
                }, 
                {
                    "id" : "073303",
                    "text" : "炎陵县",
                    "children" : null
                }, 
                {
                    "id" : "073304",
                    "text" : "茶陵县",
                    "children" : null
                }, 
                {
                    "id" : "073305",
                    "text" : "攸县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0734",
            "text" : "衡阳",
            "children" : [ 
                {
                    "id" : "073400",
                    "text" : "衡阳市区",
                    "children" : null
                }, 
                {
                    "id" : "073401",
                    "text" : "衡阳县",
                    "children" : null
                }, 
                {
                    "id" : "073402",
                    "text" : "衡南县",
                    "children" : null
                }, 
                {
                    "id" : "073403",
                    "text" : "衡山县",
                    "children" : null
                }, 
                {
                    "id" : "073404",
                    "text" : "衡东县",
                    "children" : null
                }, 
                {
                    "id" : "073405",
                    "text" : "常宁市",
                    "children" : null
                }, 
                {
                    "id" : "073406",
                    "text" : "祁东县",
                    "children" : null
                }, 
                {
                    "id" : "073407",
                    "text" : "耒阳市",
                    "children" : null
                }, 
                {
                    "id" : "073408",
                    "text" : "南岳区",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "7350",
            "text" : "郴州",
            "children" : [ 
                {
                    "id" : "073500",
                    "text" : "郴州市区",
                    "children" : null
                }, 
                {
                    "id" : "073501",
                    "text" : "北湖区",
                    "children" : null
                }, 
                {
                    "id" : "073502",
                    "text" : "苏仙区",
                    "children" : null
                }, 
                {
                    "id" : "073503",
                    "text" : "资兴市",
                    "children" : null
                }, 
                {
                    "id" : "073504",
                    "text" : "宜章县",
                    "children" : null
                }, 
                {
                    "id" : "073505",
                    "text" : "汝城县",
                    "children" : null
                }, 
                {
                    "id" : "073506",
                    "text" : "安仁县",
                    "children" : null
                }, 
                {
                    "id" : "073507",
                    "text" : "嘉禾县",
                    "children" : null
                }, 
                {
                    "id" : "073508",
                    "text" : "临武县",
                    "children" : null
                }, 
                {
                    "id" : "073509",
                    "text" : "桂东县",
                    "children" : null
                }, 
                {
                    "id" : "073510",
                    "text" : "永兴县",
                    "children" : null
                }, 
                {
                    "id" : "073511",
                    "text" : "桂阳县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0736",
            "text" : "常德",
            "children" : [ 
                {
                    "id" : "073600",
                    "text" : "常德市区",
                    "children" : null
                }, 
                {
                    "id" : "073601",
                    "text" : "石门县",
                    "children" : null
                }, 
                {
                    "id" : "073602",
                    "text" : "桃源县",
                    "children" : null
                }, 
                {
                    "id" : "073603",
                    "text" : "武陵区",
                    "children" : null
                }, 
                {
                    "id" : "073604",
                    "text" : "鼎城区",
                    "children" : null
                }, 
                {
                    "id" : "073605",
                    "text" : "津市市",
                    "children" : null
                }, 
                {
                    "id" : "073606",
                    "text" : "安乡县",
                    "children" : null
                }, 
                {
                    "id" : "073607",
                    "text" : "汉寿县",
                    "children" : null
                }, 
                {
                    "id" : "073608",
                    "text" : "澧 县",
                    "children" : null
                }, 
                {
                    "id" : "073609",
                    "text" : "临澧县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0737",
            "text" : "益阳",
            "children" : [ 
                {
                    "id" : "073701",
                    "text" : "桃江",
                    "children" : null
                }, 
                {
                    "id" : "073702",
                    "text" : "安化",
                    "children" : null
                }, 
                {
                    "id" : "073704",
                    "text" : "南县",
                    "children" : null
                }, 
                {
                    "id" : "073705",
                    "text" : "沅江",
                    "children" : null
                }, 
                {
                    "id" : "073706",
                    "text" : "赫山",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0738",
            "text" : "娄底",
            "children" : [ 
                {
                    "id" : "073800",
                    "text" : "娄底市区",
                    "children" : null
                }, 
                {
                    "id" : "073801",
                    "text" : "涟源市",
                    "children" : null
                }, 
                {
                    "id" : "073802",
                    "text" : "冷水江市",
                    "children" : null
                }, 
                {
                    "id" : "073803",
                    "text" : "新化县",
                    "children" : null
                }, 
                {
                    "id" : "073804",
                    "text" : "双峰县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0739",
            "text" : "邵阳",
            "children" : [ 
                {
                    "id" : "073900",
                    "text" : "邵阳市区",
                    "children" : null
                }, 
                {
                    "id" : "073901",
                    "text" : "邵东县",
                    "children" : null
                }, 
                {
                    "id" : "073902",
                    "text" : "洞口县",
                    "children" : null
                }, 
                {
                    "id" : "073903",
                    "text" : "新邵县",
                    "children" : null
                }, 
                {
                    "id" : "073904",
                    "text" : "绥宁县",
                    "children" : null
                }, 
                {
                    "id" : "073905",
                    "text" : "新宁县",
                    "children" : null
                }, 
                {
                    "id" : "073906",
                    "text" : "邵阳县",
                    "children" : null
                }, 
                {
                    "id" : "073907",
                    "text" : "隆回县",
                    "children" : null
                }, 
                {
                    "id" : "073908",
                    "text" : "城步苗族自治县",
                    "children" : null
                }, 
                {
                    "id" : "073909",
                    "text" : " 武冈县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0743",
            "text" : "湘西",
            "children" : [ 
                {
                    "id" : "074301",
                    "text" : "吉首市",
                    "children" : null
                }, 
                {
                    "id" : "074302",
                    "text" : "古丈县",
                    "children" : null
                }, 
                {
                    "id" : "074303",
                    "text" : "龙山县",
                    "children" : null
                }, 
                {
                    "id" : "074304",
                    "text" : "永顺县",
                    "children" : null
                }, 
                {
                    "id" : "074305",
                    "text" : "凤凰县",
                    "children" : null
                }, 
                {
                    "id" : "074306",
                    "text" : "泸溪县",
                    "children" : null
                }, 
                {
                    "id" : "074307",
                    "text" : "保靖县",
                    "children" : null
                }, 
                {
                    "id" : "074308",
                    "text" : "花垣县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0744",
            "text" : "张家界",
            "children" : [ 
                {
                    "id" : "074400",
                    "text" : "张家界市区",
                    "children" : null
                }, 
                {
                    "id" : "074402",
                    "text" : "武陵源区",
                    "children" : null
                }, 
                {
                    "id" : "074403",
                    "text" : "慈利县",
                    "children" : null
                }, 
                {
                    "id" : "074404",
                    "text" : "桑植县",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0745",
            "text" : "怀化",
            "children" : [ 
                {
                    "id" : "074500",
                    "text" : "怀化市区",
                    "children" : null
                }, 
                {
                    "id" : "074501",
                    "text" : "洪江市",
                    "children" : null
                }, 
                {
                    "id" : "074502",
                    "text" : "沅陵县",
                    "children" : null
                }, 
                {
                    "id" : "074503",
                    "text" : "辰溪县",
                    "children" : null
                }, 
                {
                    "id" : "074504",
                    "text" : "溆浦县",
                    "children" : null
                }, 
                {
                    "id" : "074505",
                    "text" : "中方县",
                    "children" : null
                }, 
                {
                    "id" : "074506",
                    "text" : "会同县",
                    "children" : null
                }, 
                {
                    "id" : "074507",
                    "text" : "麻阳苗族自治县",
                    "children" : null
                }, 
                {
                    "id" : "074508",
                    "text" : "新晃侗族自治县",
                    "children" : null
                }, 
                {
                    "id" : "074509",
                    "text" : "芷江侗族自治县",
                    "children" : null
                }, 
                {
                    "id" : "074510",
                    "text" : "靖州苗族侗族自治县",
                    "children" : null
                }, 
                {
                    "id" : "074511",
                    "text" : "通道侗族自治县",
                    "children" : null
                }, 
                {
                    "id" : "074512",
                    "text" : "洪江区",
                    "children" : null
                }
            ]
        }, 
        {
            "id" : "0746",
            "text" : "永州",
            "children" : [ 
                {
                    "id" : "074600",
                    "text" : "永州市区",
                    "children" : null
                }, 
                {
                    "id" : "074601",
                    "text" : "祁阳县",
                    "children" : null
                }, 
                {
                    "id" : "074602",
                    "text" : "蓝山县",
                    "children" : null
                }, 
                {
                    "id" : "074603",
                    "text" : "宁远县",
                    "children" : null
                }, 
                {
                    "id" : "074604",
                    "text" : "新田县",
                    "children" : null
                }, 
                {
                    "id" : "074605",
                    "text" : "东安县",
                    "children" : null
                }, 
                {
                    "id" : "074606",
                    "text" : "江永县",
                    "children" : null
                }, 
                {
                    "id" : "074607",
                    "text" : "道县",
                    "children" : null
                }, 
                {
                    "id" : "074608",
                    "text" : "双牌县",
                    "children" : null
                }, 
                {
                    "id" : "074609",
                    "text" : "江华瑶族自治县",
                    "children" : null
                }
            ]
        }
    ]
}

2、mongodb查询语句:
db.getCollection('regioncodes').aggregate([
{$unwind:"$children"},
{$unwind:"$children.children"},
{ 
                          $match: {
                                            "children.children.id":"073103"
                                         
                                  }
                        },
{$group:{
        _id:{cityId:"$children.id",cityName:"$children.text",hesinNo:"$children.children.id",hesinName:"$children.children.text"}
    }
}
]);

你可能感兴趣的:(mongodb)