使用Spring的MongoDB聚合查询

时间:2018-10-08 07:24:35

标签: spring mongodb aggregation

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"
        },
    }
}}
])

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

任何人都可以在春季帮助我编写以上查询。 我已经尝试了很多但是失败了。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容。

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")