怎么在Spring中聚合数据mongo db嵌套对象并避免PropertyReferenceException?

本教程将介绍如何在Spring中聚合数据mongo db嵌套对象并避免PropertyReferenceException?的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

怎么在Spring中聚合数据mongo db嵌套对象并避免PropertyReferenceException? 教程 第1张

问题描述

我正在SpringBoot中创建一个新的端点,它将返回从Mongo数据库中的聚合查询生成的用户的简单统计信息。然而,我得到一个PropertyReferenceException。我已经阅读了有关它的多个堆栈溢出问题,但没有找到解决此问题的问题。

我们有这样的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<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确实是因为attributesMap<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() {}

  /* ... */
}

通过在聚合中使用我的新类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 {

/* ... */
}

尽管如此,它最终还是成功了!

我想这不是最好的解决方案,我希望我不需要创建新的配置文件对象,只将attributes视为mongoTemplate查询中的StatsAttributes.class。如果有人知道怎么操作,请分享?

好了关于怎么在Spring中聚合数据mongo db嵌套对象并避免PropertyReferenceException?的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。