Spring Data MongoDB 使用group和unwind实现对数组数据进行简单统计
先简单交代一下需求:新高考政策下,高中学生可以组合选择自己喜欢的三门副科,选科结束后教务需要看到这次选科的单科统计信息。数据库中保存的数据结构如下:
{
"_id" : ObjectId("599bc8be5ae9fb99b2ca2499"),
"_class" : "com.xxx.db.model.SelectCourseRecord",
"selectCourse" : "599bc8be5ae9fb99b2ca2498",
"student" : "5993b03f8a4f1a5ba4e05d08",
"courses" : [
"地理",
"物理",
"政治"
],
"removed" : false,
"status" : "success",
"createdTime" : NumberLong(1503381694777)
},
{
"_id" : ObjectId("599bc8be5ae9fb99b2ca249a"),
"_class" : "com.xxx.db.model.SelectCourseRecord",
"selectCourse" : "599bc8be5ae9fb99b2ca2498",
"student" : "599a8faf8a4f1a26dcfef389",
"courses" :