【问题标题】:How to aggregate in spring data mongo db a nested object and avoid a PropertyReferenceException?如何在spring数据mongodb中聚合一个嵌套对象并避免PropertyReferenceException?
【发布时间】:2020-01-14 13:09:21
【问题描述】:

我正在 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.

据我了解,似乎由于attributesMap&lt;String, String&gt;,因此可能存在示意图问题。同时我不能修改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() {}

  /* ... */
}

【问题讨论】:

    标签: java mongodb spring-boot spring-data spring-data-mongodb


    【解决方案1】:

    我找到了一个解决方案,它结合了两个问题:

    PropertyReferenceException 确实是因为 attributesMap&lt;String, String&gt; 这意味着 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() {}
    
      /* ... */
    }
    

    因此,我在聚合中使用我的新类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 {
    
    /* ... */
    }
    

    所有这些,终于成功了!

    我想这不是最漂亮的解决方案,我希望我不需要创建新的 Profile 对象,只需在 mongoTemplate 查询中将attributes 视为StatsAttributes.class。如果有人知道怎么做,请分享?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-10
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 2018-09-10
      • 2017-07-14
      • 1970-01-01
      相关资源
      最近更新 更多