MongoDB统计文档(Document)的数组(Array)中的各个元素出现的次数
2017-12-01 20:52
417 查看
一,问题描述
【使用 unwind 操作符 “解包” Document 里面的Array中的每个元素,然后使用 group 分组统计,最后使用 sort 对分组结果排序】
从 images.json 文件中导入数据到MongoDB服务器
其中Document的示例如下:
现在要统计: 所有Document中的 tags 数组里面的每个元素 出现的次数。即:"kittens"出现了多少次?"travel"出现了多少次?"dogs"出现了多少次?……
二,实现步骤
使用MongoDB的Aggregate操作进行实现
①使用 unwind 分解 tags 数组,得到的结果如下:
②将分解后的每个 tag 进行 group 操作
对于group操作而言,_id 指定了 分组 的字段(对哪个字段进行 group by 操作),分组操作之后生成的结果由 num_of_tag 字段标识
③使用 project 去掉不感兴趣的 _id 字段(其实这里是将 _id 字段名 替换为 tags 字段名)(这一步可忽略)
project操作,_id:0 表示去掉_id 字段;tags:"$_id",将 _id 字段值 使用tags 字段标识;num_of_tag:1 保留 num_of_tag 字段
④使用 sort 对 num_of_tag 字段排序
三,总结
本文是MongoDB University M101课程 For Java Developers中的一次作业。结合Google搜索和MongoDB的官方文档,很容易就能实现MongoDB的各种组合查询。
相关MongoDB文章:
MongoDB 更新数组中的元素
MongoDB 组合多个条件查询($and、$in、$gte、$lte)
原文:http://www.cnblogs.com/hapjin/p/7944404.html
【使用 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
相关文章推荐
- 统计数组各个元素的出现的次数
- Array.prototype.reduce统计数组中各元素出现次数
- 统计一个无序数组中出现次数最多的元素
- c++在数组中查找元素并返回位置下标,统计某元素出现次数程序
- 统计数组arr中的值等于item的元素出现的次数
- Linux 统计文档中各个字母出现的次数,显示各个字母出现的频率
- 统计数组 arr 中值等于 item 的元素出现的次数
- 统计一个无序数组中出现次数最多的元素
- 统计`数组`元素出现的次数
- JavaScript 数组去重并统计重复元素出现的次数
- 在有序数组中,统计某一元素出现的次数
- 去除数组中重复项,并统计重复出现次数最多的元素及重复次数
- 统计数组中不同元素出现的次数(时间复杂度O(n),空间复杂度o(1))
- 统计一维数组中元素出现次数
- 统计数组中元素出现的次数——Java小程序
- 给定字符串数组,用map的key保存数组中字符串元素,value保存字符串元素出现次数,最后统计个字符串元素出现次数
- python 统计数组中元素出现次数并进行排序
- Java编程实现统计数组中各元素出现次数的方法
- 统计数组 arr 中值等于 item 的元素出现的次数
- JavaScript 数组去重并统计重复元素出现的次数实例