如何在 spring 数据 mongo db 中聚合嵌套对象并避免 PropertyReferenceException?

How to aggregate in spring data mongo db a nested object and avoid a PropertyReferenceException?

我正在 springboot 中创建一个新端点,它将 return 简单的用户统计数据从 mongo 数据库中的聚合查询生成。但是我得到了 PropertyReferenceException。我已经阅读了多个关于它的 Whosebug 问题,但没有找到解决这个问题的问题。

我们有这样的 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 class 的情况下解决这个问题,我必须创建一个新的自定义 class,它将 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() {}

  /* ... */
}

因此,我使用聚合中的新 class StatsAggregation 消除了 PropertyReferenceException 的问题:

AggregationResults<Stats> groupResults 
     = mongoTemplate.aggregate(agg, StatsProfile.class, Stats.class);

但是我不会得到任何结果。查询似乎在数据库中找不到任何文档。这就是我意识到生产 mongo 对象具有与 Profile 对象相关联的字段 "_class: com.company.dao.model.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 a StatsAttributes.class somehow in the mongoTemplate query. If anyone knows how to, please share