我正在 springboot 中创建一个新端点,它将返回从 mongo 数据库中的聚合查询生成的用户的简单统计信息。但是我得到了 PropertyReferenceException
。我已经阅读了多个有关它的 stackoverflow 问题,但没有找到解决此问题的方法。
我们有一个像这样的 mongo 数据方案:
{
"_id" : ObjectId("5d795993288c3831c8dffe60"),
"user" : "000001",
"name" : "test",
"attributes" : {
"brand" : "Chrome",
"language" : "English" }
}
数据库充满了多个用户,我们希望使用 Springboot 聚合每个 brand
的用户统计信息。 attributes
中可以有任意数量的属性。对象。
这是我们正在做的聚合
Aggregation agg = newAggregation(
group("attributes.brand").count().as("number"),
project("number").and("type").previousOperation()
);
AggregationResults<Stats> groupResults
= mongoTemplate.aggregate(agg, Profile.class, Stats.class);
return groupResults.getMappedResults();
这会产生这个有效的 mongo 查询:
> db.collection.aggregate([
{ "$group" : { "_id" : "$attributes.brand" , "number" : { "$sum" : 1}}} ,
{ "$project" : { "number" : 1 , "_id" : 0 , "type" : "$_id"}} ])
{ "number" : 4, "type" : "Chrome" }
{ "number" : 2, "type" : "Firefox" }
但是,在运行简单的集成测试时,我们收到此错误:
org.springframework.data.mapping.PropertyReferenceException: No property brand found for type String! Traversed path: Profile.attributes.
据我了解,似乎是从attributes
开始是 Map<String, String>
可能存在原理图问题。同时我无法修改 Profile
目的。
聚合中是否缺少某些内容,或者我可以在 Stats
中更改任何内容对象?
作为引用,以下是我们正在使用的用于处理 JSON 和 jackson 的数据模型。
Stats
数据模型:
@Document
public class Stats {
@JsonProperty
private String type;
@JsonProperty
private int number;
public Stats() {}
/* ... */
}
Profile
数据模型:
@Document
public class Profiles {
@NotNull
@JsonProperty
private String user;
@NotNull
@JsonProperty
private String name;
@JsonProperty
private Map<String, String> attributes = new HashMap<>();
public Stats() {}
/* ... */
}
最佳答案
我找到了一个解决方案,它结合了两个问题:
PropertyReferenceException
确实是因为attributes
引起的是 Map<String, String>
这意味着 Mongo 没有方案。
错误消息No property brand found for type String! Traversed path: Profile.attributes.
意味着 Map
对象中没有品牌属性。
为了解决这个问题而不触及我原来的Profile
类,我必须创建一个新的自定义类来映射 attributes
到具有我想要聚合的属性的属性对象,例如:
public class StatsAttributes {
@JsonProperty
private String brand;
@JsonProperty
private String language;
public StatsAttributes() {}
/* ... */
}
然后我创建了一个自定义 StatsProfile
这将利用我的 StatsAttributes
并且将类似于原来的 Profile
对象而不修改它。
@Document
public class StatsProfile {
@JsonProperty
private String user;
@JsonProperty
private StatsAttributes attributes;
public StatsProfile() {}
/* ... */
}
这样我就解决了 PropertyReferenceException
的问题使用我的新类(class)StatsAggregation
在聚合中:
AggregationResults<Stats> groupResults
= mongoTemplate.aggregate(agg, StatsProfile.class, Stats.class);
但是我不会得到任何结果。看来查询在数据库中找不到任何文档。这就是我意识到生产 mongo 对象具有字段 "_class: com.company.dao.model.Profile"
的地方。与 Profile
相关联对象。
经过一番研究,对于新的StatsProfile
要工作它需要是 @TypeAlias("Profile")
。环顾四周后,我发现我还需要精确的集合名称,这将导致:
@Document(collection = "profile")
@TypeAlias("Profile")
public class StatsProfile {
/* ... */
}
尽管如此,终于成功了!
I suppose that's not the prettiest solution, I wish I would not need to create a new Profile object and just consider the
attributes
as aStatsAttributes.class
somehow in the mongoTemplate query. If anyone knows how to, please share 🙏
关于java - 如何在 spring data mongodb 中聚合嵌套对象并避免 PropertyReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57910383/