`

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"}
    }
}
]);
分享到:
评论

相关推荐

    Python爬取数据并存入MongoDB

    本人是做JavaWeb的,可能多多少少还是遗留了Java的一些格式及规范,但爬虫千千万,却是Python最好使 Beautiful Soup4作为HTML/XML的解析器,其使用、解析难度都较为简单; 人性化的API,支持 lxml 的 XML解析器; ...

    mongodb聚合_动力节点Java学院整理

    今天跟大家分享一下mongodb中比较好玩的知识,主要包括:聚合,游标。 一:聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce。 <1> count count是最简单,最容易,也是最常用的聚合工具...

    MongoDB权威指南(中文版)高清

    1239.2 副本集 1249.2.1 初始化副本集 1259.2.2 副本集中的节点 1279.2.3 故障切换和活跃节点选举 1289.3 在从服务器上执行操作 1299.3.1 读扩展 1309.3.2 用从节点做数据处理 1309.4 工作原理 1309...

    电子书:MongoDB权威指南(中文版)

    263.3.2 使用修改器 273.3.3 Upsert 363.3.4 更新多个文档 383.3.5 返回已更新的文档 383.4 瞬间完成 413.4.1 安全操作 413.4.2 捕获“常规”错误 423.5 请求和连接 43第4 章 查询 454.1 find ...

    《10天掌握MongoDB》2012完整版.pdf[带书签]

    《10天掌握MongoDB》2012翻新完整版,大小 4.6 MB,NewSQL 中文社区系列课程。本版本带有 PDF 书签,方便读者朋友阅览。 目录: 第一天:认识MONGODB 从NOSQL说起 NOSQL数据库特点 NOSQL数据库类型 MONGODB简介 ...

    mongodb-win32-x86_64-2008plus-ssl-v3.4-latest-signed.msi

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

    node-mingydb:mongodb 的内联压缩

    允许您以最佳缩小方式查询、更新、索引、管理、聚合和以其他方式干扰 MongoDB 实例,而不会牺牲易读性、理智或山羊。 mingydb是早期的软件。 它具有可观的测试覆盖率,但到目前为止还没有在野外经历过重要的测试。...

    Mongodb-Nodejs:使用Mongodb和Nodejs

    Mongodb的Nodejs 使用测试驱动开发方法与Mongodb和Nodejs一起使用。 repo文件夹包含mongodb CRUD操作和聚合函数文件。 运行节点app.js进行工作测试

    live-data-visualization-mongodb-demo:从智能手机收集加速度计数据并在浏览器中实时可视化

    数据也保存到 mongoDB,我们准备了一些短期和长期(更)期限的实时聚合示例。 这是一个有趣的演讲,强调利用实时数据制作某些东西的容易性。 包括您所需要的一切,您可以在连接到投影仪的 MacBook 上运行它。 是的...

    第二课:mongodb企业级应用管理1

    2.mapRedurce 聚合mapRedurce 说明: 为什么需要 MapReduce 3.复制集群选举操作为了保证高可用,在集群当中如果主节点挂掉后

    SocialCG:用于 CasparCG 服务器的实时 Web 客户端,以促进社交媒体聚合和直播图形自动化。 用节点和角度构建

    用于 CasparCG 服务器的实时 Web 客户端,以促进社交媒体聚合和直播图形自动化。 使用的技术 节点.js AngularJS MongoDB socket.io 要求 安装 安装并运行 安装 获取 SocialCG git clone ...

    newsAggregator:从不同来源收集新闻,保存到mongoDB并用Angular.js显示

    简单的新闻聚合网站,显示跨多个来源的最新新闻。它将在网页上显示来自BBC 、 SKY和Hacker News的前 10 名头条新闻。 要求: node.js服务器。 存储在 MongoDb 中的所有数据。 ##项目概述 要实时查看项目,只需...

    crud-alc:这是Andela Alc项目的官方仓库

    我之所以选择GraphQL,是因为它具有从多个集合发送数据而无需不必要聚合的功能,因此路由配置更容易,API的数据消耗也更好。 前端 实现CSS 这是一个简单的演示,所以我只是使用CSS框架来简化开发。 运行应用程序 ...

    node-react-ecommerce:通过React&Node和MongoDB建立像Amazon这样的电子商务网站

    React&Node教程-5小时内全面... MongoDB:猫鼬,聚合 发展:ESLint,Babel,Git,Github, 部署:Heroku 观看React和Node教程 本地运行 1.克隆仓库 $ git clone git@github.com:basir/node-react-ecommerce.git $

    ecommerceAmazona:使用React,Redux和Node进行电子商务

    MongoDB:猫鼬,聚合 发展:ESLint,Babel,Git,Github, 部署:Heroku 观看React和Node教程 本地运行 1.克隆仓库 $ git clone git@github.com:basir/amazona.git $ cd amazona 2.设置MongoDB 本地MongoDB 从...

    node-orm2:对象关系映射

    因为他们的社区更加活跃: 安装npm install ormNode.js版本支持支持:4.0 + 测试在上运行如果需要,可以在本地运行测试: npm testDBMS支持MySQL和MariaDB PostgreSQL亚马逊Redshift SQLite的MongoDB(测试版,节点6...

    Java思维导图xmind文件+导出图片

    MongoDB聚合、索引及基本执行命令 MongoDB数据分片、转存及恢复策略 MyCat MySQL主从复制及读写分离实战 MySQL+keepalived实现双主高可用方案实践 MySQL高性能解决方案之分库分表 数据库中间件初始Mycat ...

    mash.li:mash.li mashup 聚合器网站的站点源

    Mash.li 是一个内容聚合站点,用于在 SoundCloud 音频服务上定位新的音乐混搭曲目。 它每天晚上都会扫描 SoundCloud,寻找带有“混搭”标签的新曲目,并将它们添加到越来越多的歌曲库中以供挑选。 每天,从池中选出...

    VagasBRAPI:聚合来自不同来源的巴西职位的 API

    节点.js Express.js 猫鼬(mongodb) 从上面可以看到,该项目使用mongodb来存储收集的空间。 有关后端 api 架构的更多详细信息,请 运行项目 - 前端 克隆或下载项目; 从命令提示符访问项目文件夹并键入npm ...

Global site tag (gtag.js) - Google Analytics