【发布时间】:2019-06-28 01:38:20
【问题描述】:
我正在尝试使用 MongoTemplate 和聚合框架在 Spring Boot 项目中运行聚合管道。
我的查询毫无例外地执行。但是当我尝试在AggregationResults 实例上调用getMappedResults() 时,它总是给我一个空列表。但是,如果我在调试器中检查结果,我可以看到 getRawResults() 方法返回值。
我正在使用 spring-boot 版本 1.5.9.RELEASE 和 spring-boot-starter-data-mongodb 版本 2.1.2.发布
我不确定我做错了什么。
以下是聚合的代码
GroupOperation groupOperation = Aggregation.group("field1", "field2")
.count().as("count")
.max("timestamp").as("timestamp");
ProjectionOperation projectionOperation = Aggregation.project("field1", "field2", "count", "timestamp");
DBObject cursor = new BasicDBObject(10);
AggregationOptions aggregationOptions = Aggregation.newAggregationOptions().cursor(cursor).build();
Aggregation aggregation = Aggregation.newAggregation(groupOperation, projectionOperation).withOptions(aggregationOptions);
AggregationResults<Res> activities = mongoTemplate.aggregate(aggregation, "test_collection", Res.class);
以下是我尝试在其中映射结果的类
public class Res {
public String field1;
public String field2;
public Long timestamp;
public Integer count;
public Res() {
}
public Res(String field1, String field2, Long timestamp, Integer count) {
this.field1 = field1;
this.field2 = field2;
this.timestamp = timestamp;
this.count = count;
}
}
注意
如果我跳过 AggregationOptions 中的光标,我会收到以下错误
'The 'cursor' option is required, except for aggregate with the explain argument'
【问题讨论】:
标签: mongodb spring-boot spring-data spring-mongodb