springboot+mongodb 按日期分组分页查询

List<Integer> types = new ArrayList<>();  
types.add("条件1");  
types.add("条件2");  
Criteria eatCriteria = Criteria.where("_change_type").in(types).and("_downstream_user_id").is(userId);  
Sort.Order orders = new Sort.Order(Sort.Direction.DESC, "changeDate");  
  
  
Aggregation eatAggregation = Aggregation.newAggregation(  
        //查询条件  
        Aggregation.match(eatCriteria),  
        //查询项  
        Aggregation.project("_change_money","_change_type")  
                   .andExpression("substr(_change_time,0,10)").as("changeDate"),  
        //分组条件和聚合项  
        Aggregation.group("changeDate","_change_type").sum("_change_money").as("changeMoney"),  
        //排序  
        Aggregation.sort(new Sort(orders)),  
        //分页  
        Aggregation.skip(pageIndex > 1 ? (pageIndex - 1) * pageSize : 0L),  
        Aggregation.limit(pageSize));  
  
  
AggregationResults<BasicDBObject> eatOutputType = mongoTemplate.aggregate(eatAggregation, "inner_cash_change", BasicDBObject.class);  
for (DBObject obj : eatOutputType) {  
    WalletDetailsResp.WalletDetail walletDetail = new WalletDetailsResp.WalletDetail();  
    walletDetail.setChangeType(Integer.parseInt(obj.get("_change_type").toString()));  
    walletDetail.setChangeMoney(new BigDecimal(obj.get("changeMoney").toString()).divide(QRType.YUAN_TO_FEN));  
    walletDetail.setChangeDate(obj.get("changeDate").toString());  
    list.add(walletDetail);  
}  
walletDetailsResp.setWalletDetails(list);  
pageInfoBean.setPageIndex(pageIndex);  
pageInfoBean.setPageSize(pageSize);  
//pageInfoBean.setRecordTotal();  //总页数需要单独count出来
walletDetailsResp.setPageInfo(pageInfoBean);

猜你喜欢

转载自www.cnblogs.com/jianxiaopo/p/9399607.html