我正在使用MongoDB 3.2.2和Spring Data 2.1.8。我有以下带有动态data字段的文档模型:
@Data
@Accessors(chain = true)
@Document(collection = "someCollection")
public class SomeEntity implements Serializable {
@Id
String id;
//some fields
Map<String, Object> data;
}我的目标是根据data字段中的特定关键字对文档进行分组。例如,我有以下db内容:
{
"_id": "5e5f8a89b70e4123a8285aa3",
"data": {
"someField": "someValue",
}
},
{
"_id": "5e5f72fcb70e4123a8285aa2",
"data": {
"someField": "someValue",
}
},
{
"_id": "5e5d22939ce87e2fccd80973",
"data": {
"someField": "otherValue",
}
}我想使用Spring数据构建分组聚合,如下面的MongoDB查询所示:
$group: {
{
_id: "$data.someField",
count: {
$sum: 1
}
}
}我想收到以下结果:
{
_id: "someValue",
count: 2
},
{
_id: "otherValue",
count: 1
}为了实现这个目标,我将使用org.springframework.data.mongodb.core.aggregation.Aggregation的下一个分组
Aggregation.group("$data.someField").count().as("count")但是我在执行聚合的过程中遇到了一个错误:
org.springframework.data.mapping.PropertyReferenceException: No property someField found for type Object! Traversed path: SomeEntity.data.出什么问题了?有人能帮帮我吗?
附言:我也尝试过对data字段使用$replaceRoot,所以我可以按someField对文档进行分组,但它是较新的数据库版本(3.4版中的新版本)
发布于 2020-03-11 03:12:25
会不会你只有一个小小的拼写错误:No property someFiled
尝试以下操作:
group("$data.someField").count().as("count")https://stackoverflow.com/questions/60613859
复制相似问题