`

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 聚合管道(Aggregation Pipeline)

    在MongoDB中,聚合管道由多个操作符(Operator)组成,每个操作符表示一个特定的功能节点。以下是一些常用的聚合管道操作符: 1. $match:用于过滤文档,类似于SQL中的WHERE子句。$match操作符将只允许满足条件的...

    Mongodb入门教程、示例+Spring Boot完整示例+聚合.docx

    4. 如果负载的增加(需要更多的存储空间和更强的处理能力) ,它可以分布在计算机网络中的其他节点上这就是所谓的分片。 5. Mongo 支持丰富的查询表达式。查询指令使用 JSON 形式的标记,可轻易查询文档中内嵌的对象...

    MongoDB入门指南.pdf

    MongoDB还支持聚合操作,可以对数据进行分组、排序、过滤等操作。 MongoDB中的Journaling是指将数据库的操作日志记录到磁盘上,以便在系统崩溃时可以快速地恢复数据。MongoDB的原子性和事务机制可以确保数据的一致...

    mongodb优化

    针对 MongoDB 的优化是提高数据库性能的关键,这通常涉及到多个方面,包括内存配置、查询优化、索引管理和集群架构调整等。 首先,我们可以修改 MongoDB 的默认内存配置以适应更大的数据处理需求。在 MongoDB ...

    mongodb数据库jar包

    4. 强大的查询能力:提供了丰富的查询语言,允许使用复杂的查询条件和聚合操作。 5. 自动分片:数据自动分布在多个分片上,以平衡负载并处理大量数据。 接下来,Java MongoDB驱动程序是连接MongoDB的关键。这个jar...

    五、MongoDB 学习PPT

    MongoDB的高可用性体现在其复制集(replica sets)功能上,它可以在多个节点间复制数据,确保即使在单个节点故障时也能提供服务。此外,MongoDB的分片(sharding)特性允许将数据分布到多个服务器,以实现水平扩展,...

    mongodb教程

    MongoDB 使用强大的查询语言MQL(MongoDB Query Language),支持丰富的查询表达式,包括条件查询、范围查询、聚合查询等。你可以使用查询构造器来编写复杂的查询语句,如`find()`, `aggregate()`等。 5. 更新与...

    mongodb.dll 下载.zip

    5. **副本集**:为了实现高可用性,MongoDB可以设置副本集,其中至少有一个主节点和一个或多个从节点,数据自动在节点间同步。 6. **分片**:对于大数据场景,MongoDB支持分片,将数据分布在多个机器上,以提高存储...

    MongoDB入门教程 + 架构简析 + java使用MongoDB的简单程序

    3. 查询语言:MongoDB的查询语言强大,支持丰富的查询表达式和聚合操作。 4. 高可用性:通过复制集,MongoDB可以在多个节点之间复制数据,以应对单点故障。 5. 水平扩展:分片是MongoDB的水平扩展策略,将数据分布在...

    mongodb的小例子

    7. **聚合框架**:MongoDB的聚合框架允许对数据进行分析和处理,类似SQL的GROUP BY和JOIN操作。它可以用于统计、分组、计算平均值等任务。 8. **索引**:为了提高查询性能,可以创建索引。`db.collection.create_...

    mongodb实战中文pdf

    3. **查询语言**:MongoDB的查询语言(MQL)支持丰富的查询选项,包括条件查询、排序、分组和聚合,允许用户高效地获取所需数据。 4. **索引**:了解如何创建和管理索引以优化查询性能,包括单字段索引、复合索引、...

    MongoDB Days 2015 深圳 PPT 共享

    3. **分片技术**: 分片(Sharding)是MongoDB实现横向扩展的方式,通过将数据分散到多个节点上,可以处理更大规模的数据并提高读写性能。 4. **复制集**: 复制集(Replica Sets)提供了数据冗余和故障转移,确保在...

    MongoDB参考手册-新

    3. ** 查询语句**:MongoDB提供了丰富的查询语法,包括基本查询、聚合框架、正则表达式匹配等。例如,使用`find()`方法可以查找满足特定条件的文档,而`$match`、`$group`等聚合管道操作符可用于复杂的数据分析。 4...

    mongodb离线包.ra

    8. **复制集**:MongoDB的复制集提供数据冗余和高可用性,通过复制数据到多个节点,确保即使某个节点失效,服务也能继续运行。 9. **分片**:对于大数据场景,MongoDB支持分片(sharding),将数据分布在多个机器上...

    mongodb.zip

    5. **强大的查询语言**: MongoDB提供了一种丰富的查询语言,支持各种查询操作,包括简单的查找、复杂的聚合以及地理空间查询。此外,还支持索引,以优化查询性能。 6. **易于开发**: MongoDB提供了多种编程语言的...

    spring mongodb 中文文档

    Spring Data MongoDB 可以配置连接到MongoDB 的复制集,确保在主节点故障时仍能正常工作。 通过学习这篇文档,开发者可以熟练掌握Spring Data MongoDB 的各种特性,从而更高效地在Java 应用中利用MongoDB 的强大...

    MongoDB数据库-163 李兴华培训笔记.rar

    MongoDB支持丰富的查询语法,包括基本查询、正则表达式匹配、范围查询、数组查询以及使用$符的聚合操作。在聚合框架中,可以使用`$match`、`$group`、`$sort`、`$project`等管道操作处理数据,实现复杂的数据分析。 ...

    MongoDB详解加实例

    5. 动态查询:MongoDB支持丰富的查询语法,可以进行复杂的条件筛选和聚合操作。 二、MongoDB数据模型 MongoDB中的数据是以文档的形式存储在集合(Collection)中,文档由键值对组成,类似于JSON对象。一个集合可以...

    MongoDB-2.0.6-安装文件及源码

    6. **丰富的查询语言**:MongoDB拥有强大的查询语言,支持丰富的查询操作,包括基本查询、聚合框架、地理空间查询等。 7. **索引支持**:MongoDB支持多种类型的索引,如单字段索引、复合索引、文本索引等,帮助优化...

    MongoDB;学习资料

    7. **复制集(Replication)**:复制集是MongoDB提供高可用性的一种方式,通过在多个节点间复制数据,确保在单点故障时能快速恢复服务。主节点负责写入,副节点同步数据并提供读取服务。 8. **GridFS**:MongoDB的...

Global site tag (gtag.js) - Google Analytics