mongodb - 从 MongoDB 到 Spring Boot 聚合框架的聚合
问题描述
我是 Spring 和 Mongo 的新手。我正在使用 Spring Batch 获取一些报告。我的查询需要一些MongoItemReader不支持的聚合,所以我按照下面的 stackoverflow 链接扩展了该类。
如何在春季批处理中将聚合查询与 MongoItemReader 一起使用
但是我的聚合有问题。我制作了在 mongoDB 中运行良好但无法将其转换为 Spring mongo 聚合的聚合。
MongoDb 聚合按预期工作。
db.getCollection('orders').aggregate([
{$match: {orderDate: {$gt:"2021-03-15",$lt: "2021-03-17"}, "status" :{"$in": ["GREEN", "YELLOW"]}}},
{$group: {_id: {orderDate: "$orderDate", node: "$node", code1:"$code1", code2:"$code2"}, orderUnts: {$sum: 1}}},
{"$project": {orderDate:"$_id.orderDate", node:"$_id.node", code1:"$_id.code1", code2:"$_id.code2", orderUnts:"$orderUnts"}}
])
Spring Mongo 聚合导致错误。
String[] fields = {"orderDate", "node", "code1", "code2"};
String[] projectionFields = {"orderDate", "orderDate", "code1", "code2"};
MatchOperation matchOp = Aggregation.match(Criteria.where("orderDate").gt(startDate).and("orderDate").lt(endDate).and("status").in("GREEN", "YELLOW"));
GroupOperation groupOp = Aggregation.group(fields).sum("orderUnts").as("_id");
ProjectionOperation projectOp = Aggregation.project(projectionFields);
SortOperation sortOp = Aggregation.sort(Sort.by(Sort.Direction.ASC, "orderDate"));
Aggregation aggregation = Aggregation.newAggregation(matchOp, groupOp, projectOp, sortOp);
我正在低于唯一字段错误。
Caused by: java.lang.IllegalStateException: An implementation of MongoOperations is required.
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.batch.item.data.MongoItemReader.afterPropertiesSet(MongoItemReader.java:238)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1847)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784)
... 81 common frames omitted
MongoItemReader 扩展类。
@Data
public class CustomMongoItemReader<T> extends MongoItemReader<T> {
private MongoTemplate template;
private Class<? extends T> type;
private String collection;
private MatchOperation matchOperation;
private GroupOperation groupOperation;
private ProjectionOperation projectionOperation;
private SortOperation sortOperation;
private Aggregation aggregation;
@Override
protected Iterator<T> doPageRead() {
Pageable page = PageRequest.of(this.page, this.pageSize);
if(matchOperation != null && groupOperation != null) {
Aggregation agg = Aggregation.newAggregation(matchOperation,
groupOperation,
projectionOperation,
sortOperation,
Aggregation.skip(Long.valueOf(page.getPageNumber() * page.getPageSize())),
Aggregation.limit(page.getPageSize())
);
return (Iterator<T>) template.aggregate(agg, collection, this.type).iterator();
}
else {
return Collections.emptyIterator();
}
}
}
如果问题需要更多信息,请告诉我。提前致谢。
解决方案
还使用带有聚合所需属性的afterPropertiesSet()
throws覆盖,用于 Reference check 。Exception{}
MongoItemReader<T>
推荐阅读
- c++ - 如何从 C++ 调用 sigaction
- swift - 没有半透明的清晰导航栏
- c - pipe - fork - c - 为什么程序没有任何错误没有响应?
- visual-studio-code - vscode中的新flutter项目有错误
- excel - 嵌套在 UDF [可变价格和四舍五入到接近 7]
- typescript - 如何使用 TypeScript 在 vuex 中为提交添加类型检查?
- chart.js - Chart.js 在多个图表上同步图例切换
- chef-infra - 在资源 'yum_package [apache2] 上执行操作“安装”时出错
- java - 如何存储鼠标位置?
- .net - 无法在我的网页上显示 wwwroot/images 中的图像