MongoDB聚合查询到SpringDataMongoDB

时间:2017-12-05 11:50:55

标签: mongodb mongodb-query aggregation-framework spring-data-mongodb

我有以下MongoDB聚合查询,并希望它具有等效的SpringData Mongodb查询。

MongoDB聚合查询:

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 Data中的等效功能但是在第2阶段遇到了如何将“$ addFields”转换为java代码的问题。虽然我在多个网站上搜索它但找不到任何有用的东西。请参阅第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 个答案:

答案 0 :(得分:0)

Spring Data Mongodb

$addFields尚未supported

一种解决方法是pass the raw aggregation pipeline to Spring

但由于您在第1阶段之后的字段数量有限,因此您还可以将第2阶段降级为projection

{
    $project: {
        // _id is included by default
        "count" : 1, // include count
        "point" : 1, // include point
        "totalPoint": {
            $multiply: ["$point", "$count"] // compute totalPoint
        }
    }
}

我自己还没有测试过,但这个预测应该转化为:

ProjectionOperation p = project("count", "point").and("point").multiply(Fields.field("count")).as("totalPoint");

然后您可以类似地翻译第3,4和5阶段,并将整个管道传递给Aggregation.aggregate()