Reputation: 243
I have requirement to perform group by on nested fields in mongo. The second level nested field is annotated with @Field. I am using projection with groupBy. Example
ProjectionOperation projectionOperation = Aggregation.project("id")
.and("author.eid").as("user");
GroupOperation groupOperation = Aggregation.group(aggregationBy, "user").count().as("total");
Aggregation aggregation =
Aggregation.newAggregation(projectionOperation groupOperation);
AggregationResults<Document> aggregationResults = myRepository.getMongoTemplate().aggregate(aggregation, MyClass.class, Document.class);
On execution I am getting error "org.springframework.data.mapping.PropertyReferenceException: No property eid found for type User!"
public class MyClass {
User author;
}
public class User {
@Field("eid")
@JsonProperty("eid") // fasterxml
public String externalId;
}
What I can think of is when casting the aggregation result to MyClass, it is unable to find "eid" because it is annotated.
How to handle this usecase ?
Upvotes: 1
Views: 5356
Reputation: 75934
The @Field
annotation parsed to replace the pojo property with the field name.
So you should be using
ProjectionOperation projectionOperation = Aggregation.project("id")
.and("author.externalId").as("user");
the query generated will be
{ "$project" : { "user" : "$author.eid" }
Upvotes: 4