【问题标题】:Mongo query to be written to spring data query for aggregation将 Mongo 查询写入 spring 数据查询进行聚合
【发布时间】: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 不起作用。你可以在帖子中添加UserAggregate pojo 吗?
  • @Veeram 添加了 UserAggregate pojo。

标签: mongodb spring-data aggregation-framework


【解决方案1】:

您具有空值的原因是因为您的聚合响应中没有 user 字段。

假设您的User pojo 设置正确,您可以尝试以下聚合,您可以更新您的项目以适应您的 pojo 层次结构,即映射 ("userAgg").as("userAgg.user")

 Aggregation agg = newAggregation(
   sort(Sort.Direction.DESC, "contactInfo.version"),           
   group("contactInfo.contact","contactInfo.version").
       max("contactInfo.version").as("userCurrentInfo").first("$$CURRENT").as("userAgg"),
   project().and("userAgg").as("userAgg.user")
 );

 List<UserAggregate> groupResults = mongoOperations.aggregate(agg, "userDB", UserAggregate.class).getMappedResults();

重构版本:

您可以在此处更新很多内容,例如删除 UserAggregateUserAgg pojo 并直接映射到 User 并简化聚合查询以删除 max 运算符、contactInfo.version 分组键并仅使用 @ 987654330@ 和 $$ROOT 以获取排序数据的最大版本化文档。

Aggregation agg = newAggregation(
      sort(Sort.Direction.DESC, "contactInfo.version"),
      group("contactInfo.contact").first("$$ROOT").as("user"),
      project("user").andExclude("_id")
 );

List<User> groupResults = mongoOperations.aggregate(agg, User.class, User.class).getMappedResults();

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 2021-09-03
    • 1970-01-01
    • 2019-10-14
    • 2021-06-19
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多