到SpringDataMongoDB的MongoDB聚合查询
我有下面的MongoDB聚合查询,并希望有它的等效SpringData MongoDB查询 MongoDB聚合查询:到SpringDataMongoDB的MongoDB聚合查询,mongodb,mongodb-query,aggregation-framework,spring-data-mongodb,Mongodb,Mongodb Query,Aggregation Framework,Spring Data Mongodb,我有下面的MongoDB聚合查询,并希望有它的等效SpringData MongoDB查询 MongoDB聚合查询: db.response.aggregate( // Pipeline [ // Stage 1 : Group by Emotion & Month { $group: { _id: {
db.response.aggregate(
// Pipeline
[
// Stage 1 : Group by Emotion & Month
{
$group: {
_id: {
emotion: "$emotion",
category: "$category"
},
count: {
$sum: 1
},
point: {
$first: '$point'
}
}
},
// Stage 2 : Total Points
{
$addFields: {
"totalPoint": {
$multiply: ["$point", "$count"]
}
}
},
// Stage3 : Group By Category - Overall Response Total & totalFeedbacks
{
$group: {
_id: '$_id.category',
totalFeedbacks: {
$sum: "$count"
},
overallResponseTotal: {
$sum: "$totalPoint"
}
}
},
// Stage4 - Overall Response Total & totalFeedbacks
{
$project: {
_id: 1,
overallResponseTotal: '$overallResponseTotal',
maxTotalFrom: {
"$multiply": ["$totalFeedbacks", 3.0]
},
percent: {
"$multiply": [{
"$divide": ["$overallResponseTotal", "$maxTotalFrom"]
}, 100.0]
}
}
},
// Stage4 - Percentage Monthwise
{
$project: {
_id: 1,
overallResponseTotal: 1,
maxTotalFrom: 1,
percent: {
"$multiply": [{
"$divide": ["$overallResponseTotal", "$maxTotalFrom"]
}, 100.0]
}
}
}
]
);
我曾在Spring数据中尝试过它的等价物,但在如何将“$addFields”转换为java代码的第2阶段遇到了困难。虽然我在多个网站上搜索过它,但找不到任何有用的东西。请参阅我的第1阶段的等效java代码
//Stage 1 -Group By Emotion and Category and return it's count
GroupOperation groupEmotionAndCategory = Aggregation.group("emotion","category").count().as("count").first("point")
.as("point");
Aggregation aggregation = Aggregation.newAggregation(groupEmotionAndCategory);
AggregationResults<CategoryWiseEmotion> output = mongoTemplate.aggregate(aggregation, Response.class, CategoryWiseEmotion.class);
//第1阶段-按情感和类别分组并返回其计数
GroupOperation groupEmotionAndCategory=Aggregation.group(“情感”、“类别”).count().as(“计数”).first(“点”)
.as(“点”);
聚合聚合=聚合。新建聚合(groupEmotionAndCategory);
AggregationResults输出=mongoTemplate.aggregate(聚合、响应.class、CategoryWiseMootion.class);
任何帮助都将不胜感激。
$addFields
尚未由Spring Data Mongodb提供
一个解决办法是
但是,由于在阶段1之后字段的数量有限,因此也可以将阶段2降级为:
我自己还没有测试过,但这个预测应该转化为:
ProjectionOperation p = project("count", "point").and("point").multiply(Fields.field("count")).as("totalPoint");
然后,您可以类似地翻译阶段3、4和5,并将整个管道传递到
Aggregation.aggregate()
谢谢@Marc Tarin,它成功了!但现在我被困在第四阶段做乘法和除法。请帮我检查一下,然后尝试使用project(…).andExpression(“overallResponseTotal/maxTotalFrom*100.0”).as(“percent”)
.Marc Tarin,用于//阶段3:按类别分组-总体响应总计和总体反馈。我用JavaGroupOperation-groupCategoryOverAllResponseAndTotalFeedback=Aggregation.group(“id.category”).sum(“count”).as(“totalFeedbacks”).sum(“totalPoint”).as(“overallResponseTotal”)代码>。但是它给我的错误是无效引用_id.category我不知道这背后的原理,但是Spring聚合框架不接受“_id”作为某些聚合阶段的有效字段名。您必须稍加修改才能找到解决方法,可能是第2阶段中的项目类别,并在第3阶段中参考它。请记住,您仍然可以将原始聚合传递给Spring。它有助于加快开发速度,稍后当您更熟悉Spring聚合框架的dos和DoTS时,您可以再次使用它。
ProjectionOperation p = project("count", "point").and("point").multiply(Fields.field("count")).as("totalPoint");