springdata mongodb 查询二(Aggregation)

本文介绍如何使用SpringData MongoDB的Aggregation API进行复杂的聚合查询操作,包括条件匹配和分组处理,并展示了如何通过Java代码实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


springdata mongodb 使用Aggregation进行聚合查询

仅用于个人学习总结,纯属于个人理解,如有错误请谅解,顺便帮忙指出

match 匹配   直接拼好Criteria即可
  MatchOperation match =null;
  if (a){
   match=Aggregation.match(Criteria.where("aa").in(aa).and("bb").in(bb).orOperator(new  Criteria().where("cc").mod(2, 1),new Criteria().where("dd").is("dd")));
  }else {
   match=Aggregation.match(Criteria.where("aa").in(aa).and("bb").in(bb).and("cc").mod(2, 0));
  }
 

groupOperation 将结果按照ee分组返回ee组最后一条数据修改返回属性名为ff 分组count 返回属性名改为tt


  GroupOperation groupOperation = Aggregation.group("ee").last("ee").as("ff").count().as("tt");

创建查询并返回结果
  Aggregation aggregation=Aggregation.newAggregation(match,groupOperation);

collectionName查询集合名      map.class将结果封装进Map中,也可以封装进入collectionName对应实体类


 AggregationResults results = mongoTemplate.aggregate(aggregation, collectionName,Map.class);


  List list = results.getMappedResults();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值