mongo aggregate 用法记录

mongo 聚合查询查询还是很方便的,做下记录     依赖的jar是org.springframework.data.mongodb 1.9.6  低版本可能不支持.

数据结构  大概是  这是一份问卷,问卷有15题,统计

{
    "_id": ObjectId("5a30eeef84aeea4fbcd6045c"),
    "_class": "com.fjhb.elearning.model.QuestionnaireSurvey",
    "questionnaireId": "321",
    "questionnaireType": "2",
    "completeTime": ISODate("2017-12-13T09:12:15.613+0000"),
    "personMessage": {
        "phone": "13003831002",
        "jobType": NumberInt(2),
        "identity": "农村校校长、园长",
        "name": "",
        "studentId": ""
    },
    "regionDto": {
        "_id": "350102",
        "parentId": "350100",
        "path": "/350000/350100/350102",
        "sort": NumberInt(0),
        "name": "鼓楼区",
        "version": "GB/T2260-2007",
        "available": NumberInt(1),
        "beginTime": ISODate("2018-01-02T16:00:00.000+0000"),
        "endTime": ISODate("2018-01-03T16:00:00.000+0000")
    },
    "basicQuestionnaireSurvey": [{
            "_id": "1",
            "title": "1.您对本县(市、区)政府在优先发展教育,积极推动教育事业发展的总体评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "2",
            "title": "2.您对本县(市、区)政府在促进教育公平、提高教育质量的总体评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "3",
            "title": "3.您对本县(市、区)落实义务教育阶段就学生近入学政策是否满意?",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "4",
            "title": "4.您对本县(市、区)解决义务教育阶段择校问题的评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "5",
            "title": "5.您对本县(市、区)解决进城务工人员随迁子女就学问题的评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "6",
            "title": "6. 您对本县(市、区)采取措施缩小城区与农村学校之间、学校与学校之间的办学条件差异是否满意?",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "7",
            "title": "7.您对本县(市、区)中小学校校舍场所和设施设备改善情况的评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "8",
            "title": "8.您对本县(市、区)在尊师重教,改善中小学教师(包括幼儿园、中职校和特殊学校教师)待遇方面的评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "9",
            "title": "9.您对本县(市、区)中小学校长的选用、教师的聘用和校际交流工作是否满意? ",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "10",
            "title": "10. 您对本县(市、区)在推进教学改革,发展素质教育,提高教育质量方面的评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "11",
            "title": "11. 您对本县(市、区)重视中小学教师的业务培训和专业发展,提高教师教书育人、立德树人能力的评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "12",
            "title": "12. 您对本县(市、区)做好中小学校、幼儿园的科学合理布局的评价是:",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "13",
            "title": "13.您对本县(市、区)中小学校、幼儿园做好学校安全稳定工作是否满意?",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "14",
            "title": "14.您对本县(市、区)中小学校长(含幼儿园园长)的管理水平、教师的教学水平是否满意?",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        },
        {
            "_id": "15",
            "title": "15.您对本县(市、区)中小学(含幼儿园)的教学质量是否满意?",
            "basicQuestionItems": [{
                    "_id": "1",
                    "select": true,
                    "score": NumberInt(100),
                    "content": "满意"
                },
                {
                    "_id": "2",
                    "select": false,
                    "score": NumberInt(80),
                    "content": "基本满意"
                },
                {
                    "_id": "3",
                    "select": false,
                    "score": NumberInt(60),
                    "content": "一般"
                },
                {
                    "_id": "4",
                    "select": false,
                    "score": NumberInt(40),
                    "content": "不太满意"
                },
                {
                    "_id": "5",
                    "select": false,
                    "score": NumberInt(20),
                    "content": "不满意"
                }
            ]
        }
    ],
    "platformId": "300417fe04e94eb9ac59807aa6db6bb2",
    "platformVersionId": "96f9bb883e3e40a09eea69a1b5e2aaf0",
    "projectId": "2c91128b5fa5b32c016010fb1fac111c",
    "subProjectId": "2c91128b5fa5b32c016010fb1fb5111e"
}

数据结构

db.questionnaireSurvey.aggregate({"$unwind":"$basicQuestionnaireSurvey"},
{"$unwind":"$basicQuestionnaireSurvey.basicQuestionItems"},
{"$match":{"basicQuestionnaireSurvey.basicQuestionItems.select":{$eq:true}}},
{"$project":{qusetionId:"$basicQuestionnaireSurvey._id",
title:"$basicQuestionnaireSurvey.title",
satisfied:        {$cond: { if:  {$eq: ["$basicQuestionnaireSurvey.basicQuestionItems._id", "1" ] }, then: 1, else: 0 }},
basicSatisfaction:{$cond: { if: { $eq: ["$basicQuestionnaireSurvey.basicQuestionItems._id", "2" ] }, then: 1, else: 0 }},
commonly:{$cond: { if: { $eq: [ "$basicQuestionnaireSurvey.basicQuestionItems._id", "3" ] }, then: 1, else: 0 }},
notVerySatisfied:{$cond: { if: { $eq: [ "$basicQuestionnaireSurvey.basicQuestionItems._id", "4" ] }, then: 1, else: 0 }},
unSatisfied:{$cond: { if: { $eq: ["$basicQuestionnaireSurvey.basicQuestionItems._id", "5" ] }, then: 1, else: 0 }}
}},{"$group":{_id:{qusetionId:"$qusetionId",title:"$title"},satisfied:{$sum:"$satisfied"},
basicSatisfaction:{$sum:"$basicSatisfaction"},
commonly:{$sum:"$commonly"},
notVerySatisfied:{$sum:"$notVerySatisfied"},
unSatisfied:{$sum:"$unSatisfied"},
}}
);

sql

        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.unwind("$basicQuestionnaireSurvey"));
        list.add(Aggregation.unwind("$basicQuestionnaireSurvey.basicQuestionItems"));
        list.add(Aggregation.match(Criteria.where("basicQuestionnaireSurvey.basicQuestionItems.select").is(true)));
        list.add(Aggregation.project("_id").and("$basicQuestionnaireSurvey._id").as("questionId").and("$basicQuestionnaireSurvey.title").as("title").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 100));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("satisfied").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 80));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("basicSatisfaction").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 60));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("commonly").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 40));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("notVerySatisfied").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 20));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("unSatisfied").and(new AggregationExpression(){
                    @Override
                    public DBObject toDbObject(AggregationOperationContext context) {

                        DBObject condExpression = new BasicDBObject();
                        DBObject ifExpression = new BasicDBObject();
                        ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 20));
                        condExpression.put("if", ifExpression);
                        condExpression.put("then", 1);
                        condExpression.put("else", 0);
                        return new BasicDBObject("$cond", condExpression);

                    }
                })
        );
        list.add(Aggregation.group("questionId", "title").sum("satisfied").as("satisfied").sum("basicSatisfaction").as("basicSatisfaction").sum("commonly").as("commonly").sum("notVerySatisfied").as("notVerySatisfied").sum("unSatisfied").as("unSatisfied"));

        Aggregation agg = Aggregation.newAggregation(list);
        AggregationResults<MySyllabusAnswerCountKeyValue2> results = mongoTemplate.aggregate(agg, "questionnaireSurvey", MySyllabusAnswerCountKeyValue2.class);
        results.getMappedResults();

java 代码

原文地址:https://www.cnblogs.com/jinjian91/p/9205443.html

时间: 2024-10-04 08:02:35

mongo aggregate 用法记录的相关文章

CentOS 7.x 防火墙开放端口相关用法记录

CentOS 7.x 防火墙开放端口相关用法记录 前言 防火墙对服务器起到一定的保护作用,所以了解一些相关的操作是很有必要的. 在CentOS 7.x中,有了一种新的防火墙策略,FireWall , 还记得在6.x中用的还是iptables. 这几天一直在自己的服务器上瞎搞,由于关于这个防火墙的命令还没有烂熟于心,每次都需要google一下,所以就自己动手整理了一下,方便今后的使用也同时加强记忆. 基本命令 在CentOS 7.x中,防火墙的基本命令是 firewall-cmd 先通过man f

封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil

封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,代码比较简单,主要是把MongoTarget的配置.FileTarget的配置集成到类中,同时利用缓存依赖来判断是否需要重新创建Logger类,完整代码如下: using NLog; using NLog.Config; using NLog.Mongo; using NLog.Targets; using System; using System.Collections.Generic; using System.IO;

thymeleaf用法记录

参考链接:https://www.cnblogs.com/topwill/p/7434955.html 1.基本循环 <tr th:each="prod : ${prods}"> <td th:text="${prod.name}">Onions</td> <td th:text="${prod.price}">2.41</td> <td th:text="${prod.

c语言thread用法记录。

https://blog.csdn.net/hitwengqi/article/details/8015646 先是c++11之前的 1.最基础,进程同时创建5个线程,各自调用同一个函数 #include <iostream> #include <pthread.h> //多线程相关操作头文件,可移植众多平台 using namespace std; #define NUM_THREADS 5 //线程数 void* say_hello( void* args ) { cout &

awk用法记录

awk 简介 awk是一种编程语言,主要用于在linux/unix下对文本和数据进行处理,是linux/unix下的一个工具.数据可以来自标准输入.一个或多个文件,或其它命令的输出. awk的处理文本和数据的方式:逐行扫描文件,默认从第一行到最后一行,寻找匹配的特定模式的行,并在这些行上进行你想要的操作. awk分别代表其作者姓氏的第一个字母.因为它的作者是三个人,分别是Alfred Aho.Brian Kernighan.Peter Weinberger gawk是awk的GNU版本,它提供了

jquery easyui里datagrid用法记录

1.删除行方法(deleteRow) $('#ruleManagementTable').datagrid('deleteRow', 1); //1代表选中的行索引 2.删除多行数据 var rows = $('#ruleManagementTable').datagrid("getSelections"); //获取你选择的所有行 //循环所选的行 for(var i =0,l=rows.length;i<l;i++){ var index = $('#ruleManageme

unity 的日常用法记录

1.多个GameObject 的随机抽取 public GameObject[] hazards; void Fun() { // 从多个gamObj中抽取一个 GameObject o = hazards [Random.Range (0, hazards.Length)]; Vector3 p = new Vector3 (x, y, z); Quaternion q = Quaternion.identity; Instantiate(o, p, q); // 如果你要获取你创建的Obj则

bitset常用函数用法记录 (转载)

有些程序要处理二进制位的有序集,每个位可能包含的是0(关)或1(开)的值.位是用来保存一组项或条件的yes/no信息(有时也称标志)的简洁方法.标准库提供了bitset类使得处理位集合更容易一些.要使用bitset类就必须要包含相关的头文件.在本书提供的例子中,假设都使用了std::bitset的using声明: #i nclude <bitset> using std::bitset; 3.5.1  bitset的定义和初始化 表3-6列出了bitset的构造函数.类似于vector,bit

map用法记录

c++map提供了一个键值对容器,其底层实现利用了红黑树,在给定key的情况下,可以快速的找到其对应的value并进行修改(key的值是不可以发生变化的). 说明: 1.头文件 #include<map> 2.定义 map<string ,int > m1; 3.插入数据 (1) m1["a"]=1; (2)m1.insert(map<string ,int>::value::type("b",2)); (3)m1.insert(