I am trying to run aggregation pipeline in a spring boot project using MongoTemplate
and aggregation framework.
My query executes without any exception. But when I try to call the getMappedResults()
on the AggregationResults
instance, it always gives me an empty list. However, if I inspect the result in the debugger, I can see that the getRawResults()
method returns the values.
I am using spring-boot version 1.5.9.RELEASE and spring-boot-starter-data-mongodb version 2.1.2.RELEASE
I am not sure what I am doing incorrectly.
The following is the code for aggregation
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);
The following is the class in which I am trying to map the result
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;
}
}
Note
If I skip the cursor in the AggregationOptions
, I get the following error
'The 'cursor' option is required, except for aggregate with the explain argument'