【发布时间】:2017-06-28 08:24:02
【问题描述】:
我想将以下查询与 spring data mongoDB 一起使用:
db.user.aggregate([
{"$sort": {"contactInfo.version": -1}},
{$group:{_id:"$contactInfo.contact", "maxValue": {$max:"$contactInfo.version"}, "userAgg": { "$first": "$$CURRENT" }}},
{ $project : { "userAgg" : 1 ,_id : 0 }}
]);
我们有这样的用户数据:
{
"contactInfo": {
"version": "00",
"contact": "1234567890",
"addressLine1": "Street Number 1",
"addressLine2": "Newyork",
"addressLine3": "US"
},
"name": "John Smith",
"department": "IT",
"skill": "Java",
"experience": "4",
"workStatus": "Project"
}
我正在像下面那样进行聚合,但结果为空
Aggregation agg = newAggregation(
sort(Sort.Direction.DESC, "contactInfo.version"),
group("contactInfo.contact","contactInfo.version").max("contactInfo.version").as("userCurrentInfo").first("$$CURRENT").as("userAggregate"),
project("userAggregate")
);
AggregationResults<UserAggregate> groupResults = mongoOperations.aggregate(agg, "userDB", UserAggregate.class);
List<UserAggregate> result = groupResults.getMappedResults();
任何人都可以帮我获取最大版本的用户数据,用于 spring data mongoDB 中的上述查询。 添加了 UserAggregation pojo,它的样子,因为我的查询正在返回像
这样的数据 {
"userAgg" : {
"contactInfo": {
"version": "00",
"contact": "1234567890",
"addressLine1": "Street Number 1",
"addressLine2": "Newyork",
"addressLine3": "US"
},
"name": "John Smith",
"department": "IT",
"skill": "Java",
"experience": "4",
"workStatus": "Project"
}
}
public class UserAggregation
{
UserAgg userAgg;
}
public class UserAgg
{
User user;
}
【问题讨论】:
-
看起来映射到 pojo 不起作用。你可以在帖子中添加
UserAggregatepojo 吗? -
@Veeram 添加了 UserAggregate pojo。
标签: mongodb spring-data aggregation-framework