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

2018/08/01 09:57
阅读数 520
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);

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部