mongodb聚合分组分页

标注结果有多人标注,根据 fileId 字段去重取前10数

Criteria criteria = Criteria.where("userTaskId").is("E54B427EFA3A452EB34F8DC1BA190BD6");
int pageNum = 1;
int pageSize = 10;


Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(criteria),
Aggregation.group("fileId")
        .max("index").as("index").last("fileName").as("fileName"),
        Aggregation.project("_id", "fileName"),
Aggregation.sort(new Sort(Sort.Direction.DESC,"index")),
Aggregation.skip((pageNum - 1) * pageSize),//跳到第几个开始
Aggregation.limit(pageSize)//查出多少个数据
);
AggregationResults<Result> aggregate = resultRepository.aggregate(aggregation,"A0F2D943E2F64A5EA293A7CE99139EB2");
List<Result> resultList = aggregate.getMappedResults();

 

public AggregationResults aggregate(Aggregation aggregation, String taskId) {
    return resultMongoTemplate.aggregate(aggregation,taskId, Result.class);
}

 

posted @ 2021-01-19 13:58  mabiao008  阅读(1426)  评论(0编辑  收藏  举报