使用mongoTemplate进行Aggregation聚合查询

一般业务会有具体差别,主要可以参考下Aggregation的各个参数用法,以下提供两段代码使用方式

 

需求:在订单表中,根据buyerNick分组,统计每个buyerNick的电话、地址、支付总金额以及总商品数,返回结果是CustomerDetail。

  1. /*
  2. * project:列出所有本次查询的字段,包括查询条件的字段和需要搜索的字段;
  3. * match:搜索条件criteria
  4. * unwind:某一个字段是集合,将该字段分解成数组
  5. * group:分组的字段,以及聚合相关查询
  6. * sum:求和(同sql查询)
  7. * count:数量(同sql查询)
  8. * as:别名(同sql查询)
  9. * addToSet:将符合的字段值添加到一个集合或数组中
  10. * sort:排序
  11. * skip&limit:分页查询
  12. */
  13. public List<CustomerDetail> customerDetailList(Integer pageNum,String userId,String buyerNick,String itemId,List<String> phones) throws Exception{
  14. Criteria criteria = Criteria.where("userId").is(userId);
  15. Integer pageSize = 10;
  16. Integer startRows = (pageNum - 1) * pageSize;
  17. if(buyerNick != null && !"".equals(buyerNick)){
  18. criteria.and("buyerNick").is(buyerNick);
  19. }
  20. if(phones != null && phones.size() > 0){
  21. criteria.and("mobile").in(phoneList);
  22. }
  23. if(itemId != null && !"".equals(itemId)){
  24. criteria.and("orders.numIid").is(itemId);
  25. }
  26. Aggregation customerAgg = Aggregation.newAggregation(
  27. Aggregation.project("buyerNick","payment","num","tid","userId","address","mobile","orders"),
  28. Aggregation.match(criteria),
                   Aggregation.unwind("orders"),
  29. Aggregation.group("buyerNick").first("buyerNick").as("buyerNick").first("mobile").as("mobile").
  30. first("address").as("address").sum("payment").as("totalPayment").sum("num").as("itemNum").count().as("orderNum"),
  31. Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC, "totalPayment"))),
  32. Aggregation.skip(startRows),
  33. Aggregation.limit(pageSize)
  34. );
  35. List<CustomerDetail> customerList = tradeRepository.findAggregateList(new Query(criteria), userId, customerAgg,CustomerDetail.class);
  36. return customerList;
  37. }

  第二段代码用法参考:

功能描述:

  1. 当name和course同时传参时,按id分组,统计总分数
  2. 按name分组,统计相同name的总分数
  3. 按course分组,统计总分数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

public double getTotleScoreWithMongoTemplate(StudentScore studentScore) {

 

  //封装查询条件

  List<AggregationOperation> operations = new ArrayList<>();

 

  if (StringUtils.isEmpty(studentScore.getName()) && StringUtils.isEmpty(studentScore.getCourse())){

 

    //totleScore为StudentScore类中新建的属性,用于接收统计后的总分数;当然也可以使用score(或其他属性)接收

    operations.add(Aggregation.group("id").sum("score").as("totleScore"));

  }

  if (!StringUtils.isEmpty(studentScore.getName())) {

    operations.add(Aggregation.match(Criteria.where("name").is(studentScore.getName())));

    operations.add(Aggregation.group("name").sum("score").as("totleScore"));

  }

  if (!StringUtils.isEmpty(studentScore.getCourse())) {

    operations.add(Aggregation.match(Criteria.where("course").is(studentScore.getCourse())));

    operations.add(Aggregation.group("course").sum("score").as("totleScore"));

  }

  Aggregation aggregation = Aggregation.newAggregation(operations);

 

  //查询、并获取结果

  AggregationResults<StudentScore> results = mongoTemplate.aggregate(aggregation, "studentScore", StudentScore.class);

  double totleScore = results.getUniqueMappedResult().getTotleScore();

 

  return totleScore;

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值