mongodb聚合查询

List<String> planIds = planMapper.selectAllStartPlanId(null);
        List<AggregationOperation> operations = new ArrayList<>();
        operations.add(Aggregation.match(Criteria.where("planId").in(planIds).and("attendenceTime").lte(now)));
        operations.add(Aggregation.group("userId","planId")
                .max("attendenceTime").as("attendenceTime").first("memberId").as("studentId").first("planId").as("planId")
                .first("memberNumber").as("studentNumber").first("username").as("studentName")
              .first("schoolId").as("schoolId") .first("address").as("lastAttendenceAddress").first("orgEntity.className").as("className").
                first("orgEntity.depId").as("depId").first("orgEntity.depName").as("depName")

        );
        long currPage = 0,pageSize=5000;
        operations.add(Aggregation.sort(Sort.Direction.DESC, "attendenceTime"));
//        operations.add(Aggregation.skip(currPage*pageSize));
//        operations.add(Aggregation.limit(pageSize));
        Aggregation aggregation = Aggregation.newAggregation(operations);
        AggregationResults<GxyWarnEntity> g = template.aggregate(aggregation, MongoMoguAttendenceDto.class);

        //mongodb考勤中存在的数据
        List<GxyWarnEntity> unSignins = g.getMappedResults();

Executing aggregation: [ { "$match" : { "planId" : { "$in" : [ "176715c10b0960b161c6181e0532e779" , "3ffb1d4e001135129ede50d1678e5769" , "496af0ea0bdb2b93cc0c37618e0164d8" , "5274d63c8118b5918ae920b8b7ee934c" , "6d2addfc568480c3885b24dd95dba625" , "71b51a042312f3f79d02b7aa8d037829" , "7a5cb250a1024ba69f4d9cec1bdf0d55" , "9b18ecad83517b50510b05891ba9b03d" , "a2f684015bbb8ce4dec986b898213e01" , "b60ae3e53fc64d41b7ff8f3b2dc4be23" , "bb415eb63ed180a3c5429db733916aca" , "bc94d68734b804d3202ff7aafc2de913" , "bcfe591d84295d5354c2db76e107e3ad" , "c6477c9a99df81a9c706354d4833f73e" , "cbf55d8d12ad49c93e84101ad58fb89e" , "cc234b70807bb9d1ec7994760f6b6082" , "d1f133cd90bb392fd3e06745c31b8be3" , "d6d9d190d8d2c9a92dca48c2343bc1eb" , "db6921794544332510c322c748cf9a39" , "de372c4f38172841bad23d34090643a3" , "df7abbd3434ecdb4604554d400b9071d" , "e5916f5bfc580e7a0358195f76778f07" , "e683132a2fd4f06065b02e08eb457506" , "f077795af34b7fa924103f3dc8cbef40" , "f8cc095f62eddcf08aba2141604e8c66" , "f9a5542a4cbde051de19cb3e43443165" , "fc10da03eb1ea8f107ef2717cdd1bb07"]} , "attendenceTime" : { "$lte" : { "$date" : "2019-10-08T06:51:36.503Z"}}}} , { "$group" : { "_id" : { "userId" : "$userId" , "planId" : "$planId"} , "attendenceTime" : { "$max" : "$attendenceTime"} , "studentId" : { "$first" : "$memberId"} , "planId" : { "$first" : "$planId"} , "studentNumber" : { "$first" : "$memberNumber"} , "studentName" : { "$first" : "$username"} , "schoolId" : { "$first" : "$schoolId"} , "lastAttendenceAddress" : { "$first" : "$address"} , "className" : { "$first" : "$orgEntity.className"} , "depId" : { "$first" : "$orgEntity.depId"} , "depName" : { "$first" : "$orgEntity.depName"}}} , { "$sort" : { "attendenceTime" : -1}}] in collection mogu_attendance

 

 

查询分组后的组数:

List<String> planIds = planMapper.selectAllStartPlanId(null);
Criteria criteria = Criteria.where("planId").in(planIds).and("attendenceTime").lte(now);

        //第一个userId,planId分组
        GroupOperation group = Aggregation.group("userId", "planId");
        //第二个统计分组后的组数
        CountOperation count = Aggregation.count().as("total");
        Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), group, count);

        GxyWarnEntity result = template.aggregate(agg, "mogu_attendance", GxyWarnEntity.class).getUniqueMappedResult();
//        List<GxyWarnEntity> result = template.aggregate(agg, "mogu_attendance", GxyWarnEntity.class).getMappedResults();
        logger.info("---------result---------" + result.getTotal());

查询 mogu_attendance 集合中的  planId 在  List<String> planIds  中的记录,然后根据  "userId", "planId" 两个字段分组, 分组后统计组数。 把 组数 total 映射到  GxyWarnEntity 类的 

private int total; 字段中。
查询日志:
[ { "$match" : { "planId" : { "$in" : [ "176715c10b0960b161c6181e0532e779" , "237f6686e03eb5e6a89fa62de931f408"]} , "attendenceTime" : { "$lte" : { "$date" : "2019-10-16T06:53:46.386Z"}}}} , { "$group" : { "_id" : { "userId" : "$userId" , "planId" : "$planId"}}} , { "$count" : "total"}] in collection mogu_attendance
posted @ 2019-10-08 17:38  zhoudingzhao  阅读(591)  评论(0编辑  收藏  举报