一,问题描述

【使用 unwind 操作符 “解包” Document 里面的Array中的每个元素,然后使用 group 分组统计,最后使用 sort 对分组结果排序】

从 images.json 文件中导入数据到MongoDB服务器

mongoimport --drop -d test -c images images.json

其中Document的示例如下:

> db.images.find()
{ "_id" : 3, "height" : 480, "width" : 640, "tags" : [ "kittens", "travel" ] }
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : [ "cats", "sunrises", "kittens", "travel", "vacation", "work" ] }
{ "_id" : 0, "height" : 480, "width" : 640, "tags" : [ "dogs", "work" ] }
{ "_id" : 6, "height" : 480, "width" : 640, "tags" : [ "work" ] }
{ "_id" : 4, "height" : 480, "width" : 640, "tags" : [ "dogs", "sunrises", "kittens", "travel" ] }
{ "_id" : 5, "height" : 480, "width" : 640, "tags" : [ "dogs", "cats", "sunrises", "kittens", "work" ] }
{ "_id" : 7, "height" : 480, "width" : 640, "tags" : [ "dogs", "sunrises" ] }
{ "_id" : 8, "height" : 480, "width" : 640, "tags" : [ "dogs", "cats", "sunrises", "kittens", "travel" ] }

现在要统计: 所有Document中的 tags 数组里面的每个元素 出现的次数。即:"kittens"出现了多少次?"travel"出现了多少次?"dogs"出现了多少次?……

二,实现步骤

使用MongoDB的Aggregate操作进行实现

①使用 unwind 分解 tags 数组,得到的结果如下:

> db.images.aggregate(
... [
... {$unwind:"$tags"}
... ])
{ "_id" : 3, "height" : 480, "width" : 640, "tags" : "kittens" }
{ "_id" : 3, "height" : 480, "width" : 640, "tags" : "travel" }
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : "cats" }
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : "sunrises" }
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : "kittens" }
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : "travel" }
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : "vacation" }
{ "_id" : 1, "height" : 480, "width" : 640, "tags" : "work" }
{ "_id" : 0, "height" : 480, "width" : 640, "tags" : "dogs" }
{ "_id" : 0, "height" : 480, "width" : 640, "tags" : "work" }
{ "_id" : 6, "height" : 480, "width" : 640, "tags" : "work" }
{ "_id" : 4, "height" : 480, "width" : 640, "tags" : "dogs" }
{ "_id" : 4, "height" : 480, "width" : 640, "tags" : "sunrises" }
.....
.....

②将分解后的每个 tag 进行 group 操作

对于group操作而言,_id 指定了 分组 的字段(对哪个字段进行 group by 操作),分组操作之后生成的结果由 num_of_tag 字段标识

> db.images.aggregate(
... [
... {$unwind:"$tags"},
... {$group:{_id:"$tags",num_of_tag:{$sum:1}}}
... ]
... )
{ "_id" : "dogs", "num_of_tag" : 49921 }
{ "_id" : "work", "num_of_tag" : 50070 }
{ "_id" : "vacation", "num_of_tag" : 50036 }
{ "_id" : "travel", "num_of_tag" : 49977 }
{ "_id" : "kittens", "num_of_tag" : 49932 }
{ "_id" : "sunrises", "num_of_tag" : 49887 }
{ "_id" : "cats", "num_of_tag" : 49772 }

③使用 project 去掉不感兴趣的 _id 字段(其实这里是将 _id 字段名 替换为 tags 字段名)(这一步可忽略)

project操作,_id:0 表示去掉_id 字段;tags:"$_id",将 _id 字段值 使用tags 字段标识;num_of_tag:1 保留 num_of_tag 字段

> db.images.aggregate( [ {$unwind:"$tags"},{$group:{_id:"$tags",num_of_tag:{$sum:1}}},{$project:{_id:0,tags:"$_id",num_of_tag:1}} ])
{ "num_of_tag" : 49921, "tags" : "dogs" }
{ "num_of_tag" : 50070, "tags" : "work" }
{ "num_of_tag" : 50036, "tags" : "vacation" }
{ "num_of_tag" : 49977, "tags" : "travel" }
{ "num_of_tag" : 49932, "tags" : "kittens" }
{ "num_of_tag" : 49887, "tags" : "sunrises" }
{ "num_of_tag" : 49772, "tags" : "cats" }

④使用 sort 对 num_of_tag 字段排序

> db.images.aggregate( [ {$unwind:"$tags"},{$group:{_id:"$tags",num_of_tag:{$sum:1}}},{$project:{_id:0,tags:"$_id",num_of_tag:1}},{$sort:{num_of_tag:-1}} ])
{ "num_of_tag" : 50070, "tags" : "work" }
{ "num_of_tag" : 50036, "tags" : "vacation" }
{ "num_of_tag" : 49977, "tags" : "travel" }
{ "num_of_tag" : 49932, "tags" : "kittens" }
{ "num_of_tag" : 49921, "tags" : "dogs" }
{ "num_of_tag" : 49887, "tags" : "sunrises" }
{ "num_of_tag" : 49772, "tags" : "cats" }

三,总结

本文是MongoDB University M101课程 For Java Developers中的一次作业。结合Google搜索和MongoDB的官方文档,很容易就能实现MongoDB的各种组合查询。

相关MongoDB文章:

MongoDB 更新数组中的元素

MongoDB 组合多个条件查询($and、$in、$gte、$lte)

原文:http://www.cnblogs.com/hapjin/p/7944404.html

MongoDB统计文档(Document)的数组(Array)中的各个元素出现的次数的更多相关文章

  1. iOS 判断数组array中是否包含元素a,取出a在array中的下标+数组方法详解

    目前找到来4个解决办法,第三个尤为简单方便 NSArray * arr = @["]; //是否包含 "]) { NSInteger index = [arr indexOfObj ...

  2. mongodb的基本操作与插入文档(document)

    一.mongodb的基本操作: 1.查看mongodb当前所有的databases : show dbs 2.选择数据库(database) : use databaseName(该数据库不存在则会自 ...

  3. Mongodb嵌套文档的改动-利用数组改动器更新数据

    初学mongodb的可能和我一样有个疑问.mongodb是文档型的,那么假设一个文档嵌套另外一个文档,假设对这个嵌套文档进行增删改查呢. 就像例如以下这样:.怎样对auther里面的name进行增删改 ...

  4. mongodb查询文档

    说到查询,我们一般就想起了关系型数据库的查询了,比如:order by(排序).limit(分页).范围查询(大于某个值,小于某个值..,in查询,on查询,like查询等待很多),同样mongodb ...

  5. Javascript学习8 - 脚本化文档(Document对象)

    原文:Javascript学习8 - 脚本化文档(Document对象) 每个Web浏览器窗口(或帧)显示一个HTML文档,表示这个窗口的Window对象有一个document属性,它引用了一个Doc ...

  6. MongoDB数据库文档操作

    前面的话 本文将详细介绍MongoDB数据库关于文档的增删改查 数据类型 在介绍文档操作之前,首先要了解MongoDB的数据类型 MongoDB支持许多数据类型,包括 1.字符串 - 这是用于存储数据 ...

  7. MongoDB插入文档

    db.collection.insertOne() 插入单个文档.db.collection.insertMany() 插入多个文档.db.collection.insert() 插入单/多个文档.  ...

  8. PyRevit开发第一步:获取Revit文档Document

    1.安装PythonShell插件 PythonShell 2018 插件下载 交流QQ群: 17075104 新建项目后,运行功能Python Shell, 在弹出的窗口中复制或输入以下引用代码模块 ...

  9. 【ElasticSearch】:索引Index、文档Document、字段Field

    因为从ElasticSearch6.X开始,官方准备废弃Type了.对应数据库,对ElasticSearch的理解如下: ElasticSearch 索引Index 文档Document 字段Fiel ...

随机推荐

  1. 【转】从此以后谁也别说我不懂LDO了!

    LDO是个很简单的器件,但是我跟客户沟通的过程中,发现客户工程师的技术水平参差不齐,有的工程师只是follow 别人以前的设计,任何原理和设计方法都不懂,希望大家看完这篇文章都能成为LDO 专家. 第 ...

  2. 工作队列.py

    #对列模式图Work Queue背后的主要思想是避免立即执行资源密集型任务的时,需要等待其他任务完成.所以我们把任务安排的晚一些,我们封装一个任务到消息中并把它发送到队列,一个进程运行在后端发送并最终 ...

  3. 洛谷P1896 互不侵犯

    又是一道状压DP求方案数的题... 多了一个放k个的限制,于是我们把数组多开一维. f[i][j][k]表示前i行放了j个,第i行状态为k的方案数. 然后老套路DFS转移,这次要多记录一个cnt表示上 ...

  4. MAC安装JDK及环境变量配置

    1.访问Oracle官网 http://www.oracle.com,浏览到首页的底部菜单 ,然后按下图提示操作: 2.点击“JDK DOWNLOAD”按钮: 3.选择“Accept Lisence ...

  5. 通过锁字符串达到控制并发的效果C#

    lock锁的是地址 而.net有内部机制使得相同的字符串内存地址是相同的(new string)除外 下面上实验代码 using System; using System.Collections.Ge ...

  6. Flask 键盘事件

    <div class="container" style="margin-top: 300px; "> <div class="ro ...

  7. python的变量与注释

    1.         变量 变量:值会发生变化的量,与常量相对. Python有五个标准的数据类型:1. 数字(int,long,float,complex)    2.字符串    3.元组    ...

  8. 构建flutter环境并实现属于我们的hello world

    我们知道flutter和react-native一样,都是既可以运行在andorid也可以运行在iOS环境下的. 我之前是react-native开发者,我的电脑环境中已经安装好了jdk,sdk,以及 ...

  9. Sublime Text3—软件安装、package control插件管理

    一.简介 市面上的编辑器纷繁复杂各有优点,好用的编辑器能让你工作事半功倍,先简单介绍下本文主角Sublime Text编辑器,下面简称Sublime. 1.可用于 Windows.Mac OS X 和 ...

  10. hdu 3415"Max Sum of Max-K-sub-sequence"(单调队列)

    传送门 题意: 给出一个有 N 个数字([-1000 , 1000],N ≤ 105)的环状序列: 让你求一个和最大的连续子序列,并记录起始点. 要求这个连续子序列的长度小于等于K,加和相同的不同区间 ...