java - 如何在 spring data mongodb 中聚合嵌套对象并避免 PropertyReferenceException?

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

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

相关文章:

java - 同步是否锁定结果集对象?

java - 跨面板对齐 Swing 组件

r - 如何将数据从 mongodb 流式传输到 R?

java - Spring 启动 : ClassNotFoundException when configuring maxUploadSize of CommonMultipartResolver

spring-boot - 即使更改后,Spring Boot 服务器端口仍可在 8080 上使用

java - 使用 SmartyStreets 但没有任何效果

Java 图形更新严重卡顿(简单的乒乓球游戏)

java - switch 中的 if 语句中断不起作用

sorting - 自定义排序顺序 - mongoDB 中的配置

javascript - 如何更新以对数组元素进行排序?