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 代码

多个字段分组的话,建议直接用Map去接收返回值 得到List<Map>

  

  

mongo aggregate 用法记录的更多相关文章

  1. Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!

    Go/Python/Erlang编程语言对比分析及示例   本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...

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

    封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,代码比较简单,主要是把MongoTarget的配置.FileTarget的配置集成到类中,同时利用缓存依赖来判断是否需要重新创 ...

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

    封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil,代码比较简单,主要是把MongoTarget的配置.FileTarget的配置集成到类中,同时利用缓存依赖来判断是否需要重新创 ...

  4. pt-kill 用法记录

    pt-kill 用法记录 # 参考资料Percona-Toolkit系列之pt-kill杀会话利器http://www.fordba.com/percona-toolkit-pt-kill.html ...

  5. CURL 用法记录

    CURL 用法记录 在工作中经常需要用到curl 命令,记录一下常用的场景 Send a POST Request with JSON Data curl -d '{"login" ...

  6. mongo学习使用记录1

    1 mongo的安装 1.添加MongoDB安装源 1.添加MongoDB安装源vim /etc/yum.repos.d/mongodb-enterprise.repo 将下列配置项写入文件 [mon ...

  7. mongo学习使用记录2 spring data

    spring data mongo 打印mongo NoSql语句 log4j.properties log4j.rootLogger=INFO, stdout log4j.logger.org.sp ...

  8. C#累加器函数Aggregate用法 讲解

    Enumerable.Aggregate 扩展方法在System.Linq命名空间中,是Enumerable类的第一个方法(按字母顺序排名),但确是Enumerable里面相对复杂的方法. MSDN对 ...

  9. mongo aggregate

    https://cnodejs.org/topic/59264f62855efbac2cf7a2f3 背景 现有1000条学生记录,结构如下: { name:String,//名称 clazz:{ty ...

随机推荐

  1. Android 通过联系人姓名查询联系人号码

    <!-- 读联系人权限 --><uses-permission android:name="android.permission.READ_CONTACTS" / ...

  2. SQLite3数据库

    #SQLite可视化管理工具(SQLite Expert Pro) SQLite特点: 1. 遵守ACID(原子性.一致性.隔离性和持久性)的关系型数据库管理系统:2. 不是一个C/S结构的数据库引擎 ...

  3. 给 Windows 文件菜单添加 "用XX程序打开" "用XX编辑" "用XX运行"

    有什么用就不用多说了,这可是个很有用的技巧.可以创造自己的文件格式,也可以给已有的文件添加多种打开方式 在注册表[HKEY_CLASSES_ROOT]下找到或者建立对应的扩展名 如果想对所有文件都生效 ...

  4. django练习题

    1.Web框架的本质是什么?为什么要有Web框架? 所有的Web应用,本质上其实就是一个socket服务端,用户端程序其实就是一个socket客户端.对于真实开发中的python web程序来说,一般 ...

  5. CS229 5.用正则化(Regularization)来解决过拟合

    1 过拟合 过拟合就是训练模型的过程中,模型过度拟合训练数据,而不能很好的泛化到测试数据集上.出现over-fitting的原因是多方面的: 1) 训练数据过少,数据量与数据噪声是成反比的,少量数据导 ...

  6. Android轮询器,RxJava Interval;

    基于RxJava实现轮询器,配合Retrofit处理网络请求轮询很好用,其它的一些轮询也都可以使用像Bannre图之类的: implementation 'io.reactivex.rxjava2:r ...

  7. window系统更新导致很多服务出错

    window7,win10,window server各版本系统中,经常会出现下载完成更新补丁后要求重启更新,此时很可能会出现很多服务失效的莫名其妙的问题,比如数据库连不上,IIS某功能不好使等等问题 ...

  8. Windows server 2008 R2充当路由器实现网络的互联

    1.路由器的工作原理 当IP子网中的一台主机发送IP分组给同一IP子网的另一台主机时,它将直接把IP分组送到网络上,对方就能收到.而要送给不同IP子网上的主机时,它要 选择一个能到达目的子网上的路由器 ...

  9. 「HNOI2015」开店(树链剖分, 主席树)

    /* 考虑将所求的值拆分 记每个点到根的路径长度为dis_i, 那么我们要求的就是\sum_{i = l} ^ r dis_i + dis[u] * (r - l + 1) - 2\sum_{i = ...

  10. 完全使用css编写复选框

    在日常的项目中,出现复选框或者单选框,应该都属于常见需求了,最开始阶段,一般只有两种可能性: 一.完全使用  <input type="checkbox" />或者&l ...