mongoTemplate聚合管道参考

文章记录如何将MongoShell中的管道操作对应翻译为spring-boot-starter-data-mongodbmongoTemplate的执行方式
首先要了解:MongoDB的聚合管道操作符分为
聚合管道阶段操作符$addFields$bucket$count$group等)官方文档地址
聚合管道运算符$avg$sum$first$filter等)官方文档地址
分别对应spring-boot-starter-data-mongodb提供的AggregationOperationAggregationExpression的实现类
掌握基本写法,只要能写出MongoShell就能轻松转换为Springboot的写法

多阶段MongoShell聚合操作示例

建议先使用MongoDB Compass软件验证聚合写法的正确性
下面Springboot的示例将此mongoshell代码转换为springboot的写法

[{
   
	$match: {
   
  		projectId: "CS202005251234567890123456789012",
  		createTime:{
   
    		$gte: 1590681600000,
    		$lte: 1590726107317
  		},
  		reportKey:{
   
    		$in: ["ctwElectricPower", "whwElectricPower", "cwElectricPower", "chwElectricPower", 		"acwElectricPower", "mwElectricPower", "ctFanFrequency", "ctRunningTime", "ctNumber", "cpSwitchStatus"]
  		}
	}
}, {
   
	$group: {
   
  		_id: "$createTime",
  		data: {
   
    		$push:{
   
      			deviceId: "$deviceId",
      			reportKey: "$reportKey",
      			reportValue: "$reportValue"
    		}
  		}
	}
}, {
   
	$sort: {
   
  		_id: -1
	}
}, {
   
	$group: {
   
  		_id: null,
  		count: {
   
    		$sum:1
  		},
  		data:{
   
    		$push: "$$ROOT"
  		}
	}
}, {
   
	$project: {
   
  		_id:0,
  		count:1,
  		data:{
   
    		$slice:["$data",1,10]
  		}
  	}
}]

maven坐标

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!对于MongoTemplate聚合查询,您可以使用Aggregation类来构建聚合查询管道。以下是一个简单的示例: ```java import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.TypedAggregation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; public class AggregationExample { private MongoTemplate mongoTemplate; public AggregationExample(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } public void performAggregation() { TypedAggregation aggregation = newAggregation( // 添加聚合操作 match(Criteria.where("fieldName").is("value")), group("groupField").count().as("count"), sort(Sort.Direction.DESC, "count") ); AggregationResults<AggregateResultClass> results = mongoTemplate.aggregate(aggregation, "collectionName", AggregateResultClass.class); List<AggregateResultClass> resultList = results.getMappedResults(); // 处理结果 for (AggregateResultClass result : resultList) { // 进行相应的操作 // result.getGroupField(); // result.getCount(); } } } // 聚合结果的POJO类 class AggregateResultClass { private String groupField; private int count; // 省略构造函数、getter和setter } ``` 请根据您的具体需求修改上述示例中的字段和条件。希望对您有所帮助!如果您有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fool_dawei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值