弹簧数据mongoDB聚集 - 组成的对象并构建DTO

发布于 2025-01-22 19:45:03 字数 2900 浏览 3 评论 0原文

我在mongodb中有以下员工数据,

{
  "_id": {
    "$oid": "625f09bb1a96bf42ff4c4006"
  },
  "employeeId": 1234,
  "email": "[email protected]",
  "firstName": "Jason",
  "lastName": "Stuart",
  "currentCTC": 1201117.61,
  "department": {
    "$ref": "department",
    "$id": {
      "$oid": "625f09bb1a96bf42ff4c4005"
    }
  }
}
{
  "_id": {
    "$oid": "625f09bb1a96bf42ff4c4006"
  },
  "employeeId": 1235,
  "email": "[email protected]",
  "firstName": "Jasons",
  "lastName": "Stuarts",
  "currentCTC": 1201117.61,
  "department": {
    "$ref": "department",
    "$id": {
      "$oid": "625f09bb1a96bf42ff4c4005"
    }
  }
}

我的spring @document看起来像这样:

// Employee.java
@Data
@Document
public class Employee {
    @Id
    private String id;
    private Long employeeId;
    private String email;
    private String firstName;
    private String middleName;
    private String lastName;
    private Gender gender;
    private double currentCTC;

    @DBRef
    private Department department;
}

// Department.java
@Document
@Data
public class Department {
    @Id
    private String id;
    private String name;
}

现在,我的要求是找到薪水部门的总和。

[
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c4006",
      "name": "Engineering"
    },
    "cost": 31894773.01
  },
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c4006",
      "name": "Marketing"
    },
    "cost": 4552325.25
  }
]

我在Spring Data中创建了这样的聚合函数:

public List<DepartmentCost> getDepartmentCosting() {
        GroupOperation groupByDepartment = group("department").sum("currentCTC").as("cost").first("$$ROOT").as("department");
        Aggregation aggregation = Aggregation.newAggregation(groupByDepartment);
        
        AggregationResults<DepartmentCost> results = mongoTemplate.aggregate(aggregation, "employee", DepartmentCost.class);
        return results.getMappedResults();
    }

我的预期departmentcost.java

@Data
@Document
public class DepartmentCost {
    @DBRef
    private Department department;
    private double cost;
}

现在尝试使用此API时,我可以正确地获取数据,但是我没有得到部门的名称。它以零。我得到的回应,例如

[
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c4006",
      "name": null,
    },
    "cost": 2241117.6100000003
  },
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c400a",
      "name": null,
    },
    "cost": 14774021.43
  },
  {
    "department": {
      "id": "625f09bc1a96bf42ff4c4013",
      "name": null,
    },
    "cost": 14879633.97
  }
]

如何在模型中扩展部门的详细信息?请帮忙..

I have the following Employee data in MongoDB

{
  "_id": {
    "$oid": "625f09bb1a96bf42ff4c4006"
  },
  "employeeId": 1234,
  "email": "[email protected]",
  "firstName": "Jason",
  "lastName": "Stuart",
  "currentCTC": 1201117.61,
  "department": {
    "$ref": "department",
    "$id": {
      "$oid": "625f09bb1a96bf42ff4c4005"
    }
  }
}
{
  "_id": {
    "$oid": "625f09bb1a96bf42ff4c4006"
  },
  "employeeId": 1235,
  "email": "[email protected]",
  "firstName": "Jasons",
  "lastName": "Stuarts",
  "currentCTC": 1201117.61,
  "department": {
    "$ref": "department",
    "$id": {
      "$oid": "625f09bb1a96bf42ff4c4005"
    }
  }
}

My Spring @Document looks like this:

// Employee.java
@Data
@Document
public class Employee {
    @Id
    private String id;
    private Long employeeId;
    private String email;
    private String firstName;
    private String middleName;
    private String lastName;
    private Gender gender;
    private double currentCTC;

    @DBRef
    private Department department;
}

// Department.java
@Document
@Data
public class Department {
    @Id
    private String id;
    private String name;
}

Now, my requirement is to find the sum of salaries Department-wise.. I need the data to be in the following way:

[
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c4006",
      "name": "Engineering"
    },
    "cost": 31894773.01
  },
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c4006",
      "name": "Marketing"
    },
    "cost": 4552325.25
  }
]

I created an aggregate function like this in Spring Data:

public List<DepartmentCost> getDepartmentCosting() {
        GroupOperation groupByDepartment = group("department").sum("currentCTC").as("cost").first("$ROOT").as("department");
        Aggregation aggregation = Aggregation.newAggregation(groupByDepartment);
        
        AggregationResults<DepartmentCost> results = mongoTemplate.aggregate(aggregation, "employee", DepartmentCost.class);
        return results.getMappedResults();
    }

And my expected DepartmentCost.java

@Data
@Document
public class DepartmentCost {
    @DBRef
    private Department department;
    private double cost;
}

Now when I try this API out, I get the data correctly, but I do not get department name. It comes as null. I get a response like

[
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c4006",
      "name": null,
    },
    "cost": 2241117.6100000003
  },
  {
    "department": {
      "id": "625f09bb1a96bf42ff4c400a",
      "name": null,
    },
    "cost": 14774021.43
  },
  {
    "department": {
      "id": "625f09bc1a96bf42ff4c4013",
      "name": null,
    },
    "cost": 14879633.97
  }
]

How can I get the department details expanded in my model? Please help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2025-01-29 19:45:03

经过几次尝试,我弄清楚了。我要做的就是这样:

GroupOperation groupByDepartment = group("department").sum("currentCTC").as("cost").first("$department").as("department");

相反:

GroupOperation groupByDepartment = group("department").sum("currentCTC").as("cost").first("$ROOT").as("department");

After a couple of attempts, I figured it out. All I had to do was this:

GroupOperation groupByDepartment = group("department").sum("currentCTC").as("cost").first("$department").as("department");

as opposed to:

GroupOperation groupByDepartment = group("department").sum("currentCTC").as("cost").first("$ROOT").as("department");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文