我找到了一个解决方案,它是两个问题的组合:
的
PropertyReferenceException的确造成的,因为
attributes是
Map<String,String>这意味着它的蒙戈没有计划。
该错误消息
No property brand found for type String! Traversed path:Profile.attributes.表示该
Map对象中没有品牌属性。
为了解决此问题而不接触我的原始
Profile类,我必须创建一个新的自定义类,该类将映射
attributes到具有我要添加的属性的attribute对象,例如:
public class StatsAttributes { @JsonProperty private String brand; @JsonProperty private String language; public StatsAttributes() {} }然后,我创建了一个自定义变量
StatsProfile,它将利用我的自定义变量,
StatsAttributes并且类似于原始
Profile对象,而无需对其进行修改。
@documentpublic class StatsProfile { @JsonProperty private String user; @JsonProperty private StatsAttributes attributes; public StatsProfile() {} }这样,我就消除了在聚合中
PropertyReferenceException使用新类的问题
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 {}有了所有这些,终于奏效了!
我想这不是最漂亮的解决方案,我希望我不需要创建一个新的Profile对象,而只是将其
attributes视为StatsAttributes.classmongoTemplate查询中的某种方式。如果有人知道怎么做,请分享



