博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java-mongodb-AggregationOutput(分组、统计)
阅读量:6254 次
发布时间:2019-06-22

本文共 5810 字,大约阅读时间需要 19 分钟。

hot3.png

比较常用的有:

•$project -  可以重构数据•$match - 可以实现类似query的功能•$limit - 限制返回个数•$skip - 同上•$unwind - 可以将一个包含数组的文档切分成多个, 比如你的文档有数组字段 A, A中有10个元素, 那么 经过 $unwind处理后会产生10个文档,这些文档只有 字段 A不同•$group - 统计操作, 还提供了一系列子命令     –$avg, $sum …•$sort  - 排序

接下来要实现4个功能:

task 1:统计上海学生平均年龄
从这个需求来讲,要实现功能要有几个步骤: 1. 找出上海的学生. 2. 统计平均年龄 (当然也可以先算出所有省份的平均值再找出上海的)。

select province, avg(age)  from student  where province = '上海' group by province

java代码:

/*创建 $match, 作用相当于query*/ DBObject match = new BasicDBObject("$match", new BasicDBObject("province", "上海")); /* Group操作*/ DBObject groupFields = new BasicDBObject("_id", "$province"); groupFields.put("AvgAge", new BasicDBObject("$avg", "$age")); DBObject group = new BasicDBObject("$group", groupFields); /* 查看Group结果 */ AggregationOutput output = connection.aggregate(match, group); // 执行 aggregation命令 System.out.println(output.getCommandResult());

 

输出结果:

"result" : [     { "_id" : "上海" , "AvgAge" : 32.09375}    ] ,             "ok" : 1.0

 

task2:统计每个省各科平均成绩

首先更具数据库文档结构,subjects是数组形式,需要先分组,然后再进行统计

主要处理步骤如下:

1.先用$unwind 拆数组 2. 按照 province, subject 分租并求各科目平均分

/* 创建 $unwind 操作, 用于切分数组*/ DBObject unwind = new BasicDBObject("$unwind", "$subjects"); /* Group操作*/ DBObject groupFields = new BasicDBObject("_id", new BasicDBObject("subjname", "$subjects.name").append("province", "$province")); groupFields.put("AvgScore", new BasicDBObject("$avg", "$subjects.scores")); DBObject group = new BasicDBObject("$group", groupFields); /* 查看Group结果 */ AggregationOutput output = connection.aggregate(unwind, group);  // 执行 aggregation命令 System.out.println(output.getCommandResult());

输出结果:

"result" : [       { "_id" : { "subjname" : "英语" , "province" : "海南"} , "AvgScore" : 58.1} ,       { "_id" : { "subjname" : "数学" , "province" : "海南"} , "AvgScore" : 60.485} ,      { "_id" : { "subjname" : "语文" , "province" : "江西"} , "AvgScore" : 55.538} ,       { "_id" : { "subjname" : "英语" , "province" : "上海"} , "AvgScore" : 57.65625} ,       { "_id" : { "subjname" : "数学" , "province" : "广东"} , "AvgScore" : 56.690} ,       { "_id" : { "subjname" : "数学" , "province" : "上海"} , "AvgScore" : 55.671875} ,      { "_id" : { "subjname" : "语文" , "province" : "上海"} , "AvgScore" : 56.734375} ,       { "_id" : { "subjname" : "英语" , "province" : "云南"} , "AvgScore" : 55.7301 } ,      .      .      .      .     "ok" : 1.0

task3:

 

将同一省份的科目成绩统计到一起( 即,期望 ‘province’:’xxxxx’, avgscores:[ {‘xxx’:xxx}, ….] 这样的形式)

要做的有一件事,在前面的统计结果的基础上,先用 project将平均分和成绩揉到一起,再按省份group,将各科目的平均分push到一块,使用 group 再次分组.

Mongo m = new Mongo("localhost", 27017);DB db = m.getDB("test");DBCollection coll = db.getCollection("student");/* 创建 $unwind 操作, 用于切分数组*/DBObject unwind = new BasicDBObject("$unwind", "$subjects");/* Group操作*/DBObject groupFields = new BasicDBObject("_id", new BasicDBObject("subjname", "$subjects.name").append("province", "$province"));groupFields.put("AvgScore", new BasicDBObject("$avg", "$subjects.scores"));DBObject group = new BasicDBObject("$group", groupFields);/* Reshape Group Result*/DBObject projectFields = new BasicDBObject();projectFields.put("province", "$_id.province");projectFields.put("subjinfo", new BasicDBObject("subjname","$_id.subjname").append("avgscore", "$AvgScore"));DBObject project = new BasicDBObject("$project", projectFields);/* 将结果push到一起*/DBObject groupAgainFields = new BasicDBObject("_id", "$province");groupAgainFields.put("avginfo", new BasicDBObject("$push", "$subjinfo"));DBObject reshapeGroup = new BasicDBObject("$group", groupAgainFields);/* 查看Group结果 */AggregationOutput output = coll.aggregate(unwind, group, project, reshapeGroup);System.out.println(output.getCommandResult());
"result" : [        { "_id" : "辽宁" , "avginfo" : [ { "subjname" : "数学" , "avgscore" : 56.46666666666667} , { "subjname" : "英语" , "avgscore" : 52.093333333333334} , { "subjname" : "语文" , "avgscore" : 50.53333333333333}]} ,        { "_id" : "四川" , "avginfo" : [ { "subjname" : "数学" , "avgscore" : 52.72727272727273} , { "subjname" : "英语" , "avgscore" : 55.90909090909091} , { "subjname" : "语文" , "avgscore" : 57.59090909090909}]} ,        { "_id" : "重庆" , "avginfo" : [ { "subjname" : "语文" , "avgscore" : 56.077922077922075} , { "subjname" : "英语" , "avgscore" : 54.84415584415584} , { "subjname" : "数学" , "avgscore" : 55.33766233766234}]} ,        { "_id" : "安徽" , "avginfo" : [ { "subjname" : "英语" , "avgscore" : 55.458333333333336} , { "subjname" : "数学" , "avgscore" : 54.47222222222222} , { "subjname" : "语文" , "avgscore" : 52.80555555555556}]}     .    .    .   ] , "ok" : 1.0}

 

task4:

DBCollection collection = MongoUtils.getCollection_Database(                    (String) ServletContextUtils.getSession().getAttribute(                            "game"), SysConst.TABLE_WAIGUA_RATIO);            // query            DBObject match = new BasicDBObject("$match", queryParam_n);            // 利用$project拼装group需要的数据            DBObject fields = new BasicDBObject("name", 1);            fields.put("count", 1);            DBObject project = new BasicDBObject("$project", fields);            DBObject groupFields = new BasicDBObject("_id", "$name");            groupFields.put("count", new BasicDBObject("$sum", "$count"));            DBObject group = new BasicDBObject("$group", groupFields);// group            DBObject limit = new BasicDBObject("$limit", Integer.parseInt(n));            DBObject sort = new BasicDBObject("$sort", new BasicDBObject(                    "count", -1));            AggregationOutput output = collection.aggregate(match, project,                    group, sort, limit);            List
nameList = new ArrayList
(); for (DBObject obj : output.results()) { BasicDBObject obj2 = (BasicDBObject) obj; nameList.add(obj2.getString("_id")); }

转载于:https://my.oschina.net/u/1458864/blog/1504738

你可能感兴趣的文章
poj 1975 Median Weight Bead(传递闭包 Floyd)
查看>>
【C++ Primer每天刷牙】一间 迭代器
查看>>
:施密特建议尾随年轻的专业人士了解技术公司
查看>>
软件项目如何选型
查看>>
UVa 11205 - The broken pedometer
查看>>
[书目20150727]选择重于一切(扎克伯格给年轻人的37个人生忠告)
查看>>
Apache与Nginx的优缺点比较
查看>>
【第二篇】Volley的使用之加载图片
查看>>
hdu oj1102 Constructing Roads(最小生成树)
查看>>
减少可执行程序size的三个常用软件
查看>>
窥探Swift之别样的枚举类型
查看>>
pyspark简要原则
查看>>
【移动开发】Android中WIFI开发总结(二)
查看>>
idea git merge代码
查看>>
云计算设计模式(二十二)——静态内容托管模式
查看>>
[Angularjs]ng-file-upload上传文件
查看>>
修改类不用重启Tomcat加载整个项目
查看>>
iframe刷新父页面
查看>>
KL46 custom board SWD reset is never asserted - SWS Waveform
查看>>
如何提高团队管理能力1
查看>>