1、条件匹配:MatchOperation
MatchOperation matchOperation = Aggregation.match(new Criteria().where("area_code").is(areaCode));
2、分组计数:GroupOperation + count
GroupOperation groupSource_typeOperation = Aggregation.group("source_type", "satisfied_degree")
.count().as("count").first("source_type").as("source_type");
3、分组求和:GroupOperation + sum
GroupOperation groupDeptOperation = groupDeptOperation = Aggregation.group("dept_name")
.sum("evaluate_quantity").as("evaluate_quantity").first("dept_name").as("dept_name");
4、排序:SortOperation
SortOperation sortOperation = Aggregation.sort(new Sort(Direction.DESC,"evaluate_quantity"));
5、跳过:SkipOperation
SkipOperation skipOperation = Aggregation.skip(10);
6、限制查多少个:LimitOperation
LimitOperation limitOperation = Aggregation.limit(10);
7、执行查询
注意,Operation的顺序一定要写对,不然查出来有差异,是按照add进list的顺序执行,直接写在入参中也是一样
List<AggregationOperation> operations = new ArrayList<>();
operations.add(matchDeptOperation);
operations.add(groupDeptOperation);
operations.add(skipOperation );
operations.add(limitOperation);
AggregationResults<Map> aggregate = mongoTemplate.aggregate(Aggregation.newAggregation(operations), EvaluateConstant.EVALUATE_COLLECTION, Map.class);