我需要在mongo中的嵌套字段上执行group by。 第二级嵌套字段使用@Field注释。我正在使用groupBy投影。 实施例
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);
执行时我收到错误&#34; org.springframework.data.mapping.PropertyReferenceException:找不到类型User的属性eid!&#34;
public class MyClass {
User author;
}
public class User {
@Field("eid")
@JsonProperty("eid") // fasterxml
public String externalId;
}
我能想到的是当将聚合结果转换为MyClass时,它无法找到&#34; eid&#34;因为它有注释。
如何处理这个用例?
答案 0 :(得分:3)
解析@Field
注释以使用字段名称替换pojo属性。
所以你应该使用
ProjectionOperation projectionOperation = Aggregation.project("id")
.and("author.externalId").as("user");
生成的查询将是
{ "$project" : { "user" : "$author.eid" }