mongodb聚合分组分页

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

Criteria criteria =  Criteria
        .where("userTaskId").is("E54B427EFA3A452EB34F8DC1BA190BD6")
        .and("deleted").is(false);

int pageNum = 1;
int pageSize = 10;

Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(criteria),
        Aggregation.group("fileId"),
        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();
System.out.println(resultList.size());

 

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

 

上一篇:MongoDB( 五 )MongoDB普通查询和聚合查询


下一篇:Spring boot MongoDB多数据源,MongoRepository实现