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);
相關文章
相關標籤/搜索