mongoTemplate 聚合分组统计,aggregate分页

    /**
    *mongoTemplate 聚合分组统计返回分组列表
    */
    public Page groupStatistics(String compCampId, Long productId, Integer page, Integer size) {
    if (page<1){page=1;}

    Integer startRows = (page - 1) * size;

    Sort sort = new Sort(Sort.Direction.DESC,"create_time");

    List patterns =new ArrayList<>();

    patterns.add("scan_code_action");
    patterns.add("pay_wechat_success");
    Criteria criteria =  Criteria.where("operation_name").in(patterns);

    if (productId != null ){
        criteria.and("module_id").is(productId);
    }

    if (compCampId != null ){
        criteria.and("activity_id").is(compCampId);
    }

    SpringDataPageable pageable = new SpringDataPageable();
    //开始页
    pageable.setPagenumber(page);
    //每页条数
    pageable.setPagesize(size);
    //排序
    pageable.setSort(sort);

    List operations = new ArrayList<>();
    operations.add(Aggregation.match(criteria));
    operations.add(Aggregation.group("order_id").max("order_status").as("order_status").max("channel_parameter").as("channel_parameter")
    .max("source").as("source").max("nickname").as("nickname").max("is_press").as("is_press").max("openid").as("openid")
    .max("burying_id").as("burying_id").max("mobile").as("mobile").max("create_time").as("create_time")
    );

    long count = mongoTemplate.aggregate(Aggregation.newAggregation(operations),"log",Log.class).getMappedResults().size();

    operations.add(Aggregation.skip(startRows));
    operations.add(Aggregation.limit(size));
    Aggregation aggregation = Aggregation.newAggregation(operations);
    aggregation.sort(sort);

    Query query = new Query();
    query.addCriteria(criteria);
    AggregationResults aggregate = this.mongoTemplate.aggregate(aggregation,"log",Log.class);
    List buryingLogs = aggregate.getMappedResults();
    Page pager = new PageImpl<>(buryingLogs,pageable,count);
    return pager;
}
 List<AggregationOperation> operations =getSalesOperations(recommendGroup, startTime, endTime, sortName, sortStr);
        long count = mongoTemplate.aggregate(Aggregation.newAggregation(operations), "order", SaleStatisticsData.class).getMappedResults().size();

        operations.add(Aggregation.skip(startRows));
        operations.add(Aggregation.limit(size));
        Aggregation aggregation = Aggregation.newAggregation(operations);
        AggregationResults<SaleStatisticsData> results = mongoTemplate.aggregate(aggregation, "order", SaleStatisticsData.class);
        List<SaleStatisticsData> list = results.getMappedResults();

你可能感兴趣的:(mongo)