Mongodb高级查询Aggregation聚合组件分页


用Mongo的聚合组件Aggregation要用到两个方法skip和limit。skip设置起点(分页的时候不包含起点,从起点的下一行开始),limit设置条数。如:

Aggregation.skip(10),

Aggregation.limit(20)的意思是(10,20)第10条以后取20条数据。

    	Criteria criteria= new Criteria();
    	if(map.get("userlist")!=null&&!"".equals(map.get("userlist"))){
    		List userList =(List) map.get("userlist");
    		criteria.and(Field.USERPRIVACY).in(userList);
    	}
    	Order orders = new Order(Direction.DESC,Field.CREATE_TIME);
    	Sort sort = new Sort(orders);
        TypedAggregation aggregation = Aggregation.newAggregation(OrderReport.class,
                Aggregation.match(
                		criteria
//                                .and(Field.CLOSE_TYPE).in(TimetableOrderCloseType.NORMAL, TimetableOrderCloseType.QUIT_FIXED_CLASS, TimetableOrderCloseType.FROZEN)
//                                .and(Field.START_TIME).gte(startTime).lte(endTime)
                ),
                Aggregation.group(Field.VIPSERVICE)
                		.first(Field.VIPSERVICE).as(Field.VIPSERVICE)
                        .first(Field.USERPRIVACY).as(Field.USERPRIVACY)
                        .first(Field.SCHOOL_ZONE).as(Field.SCHOOL_ZONE)
                        .first(Field.CHILD).as(Field.CHILD)
                        .first(Field.CREATE_TIME).as(Field.CREATE_TIME)
                        .sum(Field.NORMAL_CLASSNUMBER).as(Field.NORMAL_CLASSNUMBER)
                        .sum(Field.NORMAL_WEEKDAY_CLASSNUMBER).as(Field.NORMAL_WEEKDAY_CLASSNUMBER)
                        .sum(Field.NORMAL_WEEKEND_CLASSNUMBER).as(Field.NORMAL_WEEKEND_CLASSNUMBER)
                        .sum(Field.NORMAL_PRICE).as(Field.NORMAL_PRICE)
                        .sum(Field.NORMAL_WEEKDAY_PRICE).as(Field.NORMAL_WEEKDAY_PRICE)
                        .sum(Field.NORMAL_WEEKEND_PRICE).as(Field.NORMAL_WEEKEND_PRICE),
               Aggregation.sort(sort), 
               Aggregation.skip(page.getPage()>1?(page.getPage()-1)*page.getRows():0),
               Aggregation.limit(page.getRows())  
        );
        List consultReportSearch =CollectionUtil.safe(mongoTemplate.aggregate(aggregation, OrderReport.class).getMappedResults());


你可能感兴趣的:(mongoDb)