java-mongodb分组查询

•$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());

 
 
   
   
   
   
  • 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
 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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());
 
 
   
   
   
   
  • 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}, ….] 这样的形式)

要做的有一件事,在前面的统计结果的基础上,先用  projectgrouppush, 使用 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());
 
 
   
   
   
   
  • 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}
 
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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<String> nameList = new ArrayList<String>();
            for (DBObject obj : output.results()) {
                BasicDBObject obj2 = (BasicDBObject) obj;
                nameList.add(obj2.getString("_id"));
            }

你可能感兴趣的:(java,mongodb,java)