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);
}

 

相关文章

MongoTemplate 是Spring Data MongoDB 中的一个核心类,为 S...
笔者今天要分享的是一个项目重构过程中如何将数据库选型由原...
mongodb/mongoTemplate.upsert批量插入更新数据的实现
进入官网下载官网安装点击next勾选同意,点击next点击custom...
头歌 MongoDB实验——数据库基本操作
期末考试复习总结