此示例聚合将抛出一个IllegalArgumentException无效的引用“角色”!
每次在投影阶段后重命名字段后,我们都会遇到这个问题。
final Aggregation aggregation = newAggregation(
// We only like to have the "company" and "empolyee.role" renamed to "role"
project("company")
.and("employee.role").as("role"),
// Group by the **renamed** "role"
group("role").count().as("count"), // this will fail because "role" is an invalid reference.
limit(2)
);
return aggregation;
Run Code Online (Sandbox Code Playgroud)
我们正在处理的 JSON 如下所示:
{
// some fields
company : {
// some fields
}
employee : {
role : {
// some fields
}
}
}
Run Code Online (Sandbox Code Playgroud)
想法:
这里奥利弗说
重要的是要了解您根据类型属性而不是文档字段名称来定义聚合。
这就是我们得到异常的原因吗?如果是这样,如何使用很好的聚合 api spring 数据提供。
更新 …