•$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代码:
DBObject match = new BasicDBObject("$match", new BasicDBObject("province", "上海"));
DBObject groupFields = new BasicDBObject("_id", "$province");
groupFields.put("AvgAge", new BasicDBObject("$avg", "$age"));
DBObject group = new BasicDBObject("$group", groupFields);
AggregationOutput output = connection.aggregate(match, group);
System.out.println(output.getCommandResult());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
输出结果:
"result" : [
{ "_id" : "上海" , "AvgAge" : 32.09375}
] ,
"ok" : 1.0
task2:统计每个省各科平均成绩
首先更具数据库文档结构,subjects是数组形式,需要先分组,然后再进行统计
主要处理步骤如下:
1.先用$unwind 拆数组 2. 按照 province, subject 分租并求各科目平均分
DBObject unwind = new BasicDBObject("$unwind", "$subjects");
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);
AggregationOutput output = connection.aggregate(unwind, group);
System.out.println(output.getCommandResult());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
输出结果:
"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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
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");
DBObject unwind = new BasicDBObject("$unwind", "$subjects");
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);
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);
DBObject groupAgainFields = new BasicDBObject("_id", "$province");
groupAgainFields.put("avginfo", new BasicDBObject("$push", "$subjinfo"));
DBObject reshapeGroup = new BasicDBObject("$group", groupAgainFields);
AggregationOutput output = coll.aggregate(unwind, group, project, reshapeGroup);
System.out.println(output.getCommandResult());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
"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);
DBObject match = new BasicDBObject("$match", queryParam_n);
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);
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<String> nameList = new ArrayList<String>();
for (DBObject obj : output.results()) {
BasicDBObject obj2 = (BasicDBObject) obj;
nameList.add(obj2.getString("_id"));
}