我应该如何使用 Spring 数据 MongoDB 计算由另一个分组的字段总和
How should I calculate sum of field grouped by another one using Spring Data MongoDB
我有一个看起来像这样的用户集合-
{ "post": "teacher",
"salary": 4500
},
{ "post": "teacher",
"salary": 9000
},
{ "post": "principal",
"salary": 7000
},
{ "post": "teacher",
"salary": 4500
}
我想计算所有老师的工资总额,校长也是这样。所以我想要看起来像
的东西
"teachers_salary": 18000
"principals_salary": 7000
我想使用聚合,但没有得到所需的输出。如果你们中的任何人都可以帮助我找到解决方案,那将非常有帮助。
在 mongoshell 上你可以试试这个
db.collection_name.aggregate([ {
$group: {
_id: "$post",
totalsalary: { $sum: "$salary" }
}
} ] )
https://docs.mongodb.com/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum
这里是一个使用 spring-data-mongodb.
聚合的例子
假设结果模型对象 PostTotalSalary.class:
public class PostTotalSalary {
private String post; // "teacher" or "principal"
private Integer totalSalary;
}
我们将创建一个 GroupOperation 以使用键“post”
收集具有相同值的所有文档
GroupOperation groupByPostAndSumSalary = group("post")
.sum("salary")
.as("totalSalary");
我们现在可以使用 spring-data-mongodb 创建聚合,然后将结果映射到您的结果模型对象(假设集合名称为“posts” ):
Aggregation aggregation = Aggregation.newAggregation(groupByPostAndSumSalary);
AggregationResults<PostTotalSalary> groupResults = mongoTemplate.aggregate(aggregation, "posts", PostTotalSalary.class);
如果您需要一个列表,AggregationResults 有 getMappedResults 方法来执行此操作:
List<PostTotalSalary> mappedResults = groupResults.getMappedResults();
最终结果将是
[
{
"post" : "teacher",
"totalSalary" : "18000" // the total of all salaries for every "teacher" document
},
{
"post" : "principal",
"totalSalary" : "7000" // the total of all salaries for every "principal" document
}
]
我有一个看起来像这样的用户集合-
{ "post": "teacher",
"salary": 4500
},
{ "post": "teacher",
"salary": 9000
},
{ "post": "principal",
"salary": 7000
},
{ "post": "teacher",
"salary": 4500
}
我想计算所有老师的工资总额,校长也是这样。所以我想要看起来像
的东西"teachers_salary": 18000
"principals_salary": 7000
我想使用聚合,但没有得到所需的输出。如果你们中的任何人都可以帮助我找到解决方案,那将非常有帮助。
在 mongoshell 上你可以试试这个
db.collection_name.aggregate([ {
$group: {
_id: "$post",
totalsalary: { $sum: "$salary" }
}
} ] )
https://docs.mongodb.com/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum
这里是一个使用 spring-data-mongodb.
聚合的例子假设结果模型对象 PostTotalSalary.class:
public class PostTotalSalary {
private String post; // "teacher" or "principal"
private Integer totalSalary;
}
我们将创建一个 GroupOperation 以使用键“post”
收集具有相同值的所有文档GroupOperation groupByPostAndSumSalary = group("post")
.sum("salary")
.as("totalSalary");
我们现在可以使用 spring-data-mongodb 创建聚合,然后将结果映射到您的结果模型对象(假设集合名称为“posts” ):
Aggregation aggregation = Aggregation.newAggregation(groupByPostAndSumSalary);
AggregationResults<PostTotalSalary> groupResults = mongoTemplate.aggregate(aggregation, "posts", PostTotalSalary.class);
如果您需要一个列表,AggregationResults 有 getMappedResults 方法来执行此操作:
List<PostTotalSalary> mappedResults = groupResults.getMappedResults();
最终结果将是
[
{
"post" : "teacher",
"totalSalary" : "18000" // the total of all salaries for every "teacher" document
},
{
"post" : "principal",
"totalSalary" : "7000" // the total of all salaries for every "principal" document
}
]