[英]Spring data mongo aggregation with @Field annotation
I have requirement to perform group by on nested fields in mongo. 我需要在mongo中的嵌套字段上执行分组依据。 The second level nested field is annotated with @Field.
第二层嵌套字段用@Field注释。 I am using projection with groupBy.
我在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! " 执行时,我收到错误消息“ org.springframework.data.mapping.PropertyReferenceException:未找到类型为User的属性eid ”
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. 我能想到的是,当将聚合结果转换为MyClass时,因为有注释,所以无法找到“ eid”。
How to handle this usecase ? 如何处理这个用例?
The @Field
annotation parsed to replace the pojo property with the field name. 解析
@Field
批注以将pojo属性替换为字段名称。
So you should be using 所以你应该使用
ProjectionOperation projectionOperation = Aggregation.project("id")
.and("author.externalId").as("user");
the query generated will be 生成的查询将是
{ "$project" : { "user" : "$author.eid" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.