首页 > 解决方案 > 使用spring的MongoDB聚合查询

问题描述

db.getCollection('questionbank').aggregate([
{ "$group": {
        "_id": {
        "technology": "$technology",
       "level":"$level",
         "type":"$type"
    },
    "Count": { "$sum": 1 }
}},
{ "$group": {
    "_id": "$_id.technology",
    "QuestionCount": { 
        "$push": { 
            "level":"$_id.level",
            "type":"$_id.type",
            "count": "$Count"
        },
    }
}}
])

我正在尝试获得相同的输出结构。

谁能帮我在春天写下上面的查询。我尝试了很多但失败了。

标签: springmongodbaggregation

解决方案


您可以使用以下 .

group("technology", "level", "type").count().as("count"),            group("_id.technology")                .push(                    new BasicDBObject("level", "$_id.level")                        .append("type", "$_id.type")                        .append("count", "$count"))                .as("questionCount")

推荐阅读