数据库
首页 > 数据库> > mongodb聚合分组分页

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

 

标签:聚合,pageSize,组分,mongodb,Aggregation,aggregation,int,Criteria,aggregate
来源: https://www.cnblogs.com/mabiao008/p/14297612.html