mongoTemplate 聚合分组统计复杂条件分组sum\去重\$project\$cond\$addToSet

mongo 通道查询:

db.order.aggregate({
    $match: {
        "course_mode": {
            $gt: 2
        },
        "is_new": 1
    }
}, {

$group: {
    "_id": {
        "part_mode": "$part_mode"
        },
        "student_total": {
            $sum: 1
        },
        "student_id": {
            $addToSet: "$student_id"
        },
        "part_mode": {
            $max: "$part_mode"
        }
    }
}, {
    $project: {
        "part_mode": 1,
        "title": "新生人数累计",
        "student_total": 1,
        "students": {
            $cond: {
                if : {
                    $isArray: "$student_id"
                },
                then: {
                    $size: "$student_id"
                },
                else : 0
            }
        }
    }
}, {
    $project: {
        "part_mode": 1,
        "title": 1,
        "student_total": 1,
        "students":1,
				"rat": {
            $cond: {
                if : {
									 $gt: ['$students', 0]
                },
                then: {
                    $divide: ["$student_total", "$students"]
                },
                else : 0
            }
        }
    }
}, {
    $sort: {
        "part_mode": 1
    }
}

);

翻译java:

Sort sort = new Sort(Sort.Direction.ASC, "partMode");

        List<AggregationOperation> operations = new ArrayList<>();
        Criteria criteria = Criteria.where("course_mode").gt(2);
        if (seanson==null || seanson ==0){
            criteria.and("year").is(year).and("quarter_mode").is(quarterMode);
        }

        if (title.contains("累计")){
            criteria.and("pay_time").lt(DateUtils.getDayEnd(startTime));
        }

        if (seanson != null && seanson == 1) {

            criteria.orOperator(Criteria.where("year").gt(year), new Criteria().andOperator(Criteria.where("year").is(year), Criteria.where("quarter_mode").gt(quarterMode)));

        }


        if (isNew != null && isNew ==1){
            criteria.and("is_new").is(1);
        }

        if (dayTime != null) {
            long startTimeStr = DateUtils.getCurrentDayZeroTime(startTime);
            long endTimeStr = DateUtils.getDayEnd(startTime);
            criteria.and("pay_time").gt(startTimeStr).lt(endTimeStr);
        }

        operations.add(Aggregation.match(criteria));
        operations.add(Aggregation.group("part_mode").max("part_mode").as("partMode").addToSet("student_id").as("student_id").max("source").as("title").count().as("studentTotal"));
        ConditionalOperators.Cond cond = ConditionalOperators.when(Criteria.byExample("{$student_id: $isArray}")).then(new Document("$size", "$student_id")).otherwise(0);
        ConditionalOperators.Cond cond1 = ConditionalOperators.when(new Criteria("title").is(null)).then(new Document(title, "$title")).otherwise(title);


        if (title.contains("人次")){
            operations.add(Aggregation.project("partMode","studentTotal").and(cond1).as("title"));

        }else{
            operations.add(Aggregation.project("partMode","studentTotal").and(cond).as("students").and(cond1).as("title"));
        }

        if (isNew != null && isNew ==1 && !title.contains("人次")) {
            operations.add(Aggregation.project("partMode", "studentTotal", "title", "students").andExpression("studentTotal/students").as("joinRate"));
        }


        operations.add(Aggregation.sort(sort));
        Aggregation aggregation1 = Aggregation.newAggregation(operations);

        AggregationResults<FinanceAnalysisData> results = mongoTemplate.aggregate(aggregation1, "order", FinanceAnalysisData.class);

        List<FinanceAnalysisData> list = results.getMappedResults();

你可能感兴趣的:(mongo,数据库)