如何以数组格式mongoDb SpringBoot检索数据

时间:2018-03-19 05:31:01

标签: mongodb spring-boot spring-data spring-data-jpa mongotemplate

我的mongoDB中有数据如下

{
    "stack":"webTechnology",
    "subStack":"angular"
},
{
    "stack":"webTechnology",
    "subStack":"react"
}.
{
    "stack":"webTechnology",
    "subStack":"angular"
},
{
    "stack":"script",
    "subStack":"python"
},
{
    "stack":"script",
    "subStack":"javaScript"
},
{
    "stack":"Java",
    "subStack":"Spring"
}

我需要以下列格式将这些数据作为Rest API的响应返回, 由stack和substack分类

[
    "webTechnology":[
        "angular":[
            {
                "stack":"webTechnology",
                "subStack":"angular"
            },
            {
                "stack":"webTechnology",
                "subStack":"angular"
            }
        ],
        "react":[
            {
                "stack":"webTechnology",
                "subStack":"react"
            }
        ]
    ],
    "script":[
        "python":[
            {
                "stack":"script",
                "subStack":"python"
            }
        ],
        "javaScript":[
            {
                "stack":"script",
                "subStack":"javaScript"
            }
        ]
    ],
    "java":[
        "spring":[
            {
                "stack":"java",
                "subStack":"spring"
            }
        ]
    ]
]

我使用mongoTemplate来检索以List格式返回的数据

mongoTemplate.getCollection(collectionName).find().toArray()

如何以我期待的方式获得结果?

1 个答案:

答案 0 :(得分:1)

我将您的数据插入临时集dataCollection,这是按stacksubstack对数据进行分组的查询:

db.getCollection('dataCollection').aggregate([

    {

          $group: {
                _id: {stack: "$stack", subStack: "$subStack"},
                result: { $push : "$$ROOT" }

              }

    }

])

将查询转换为mongotemplate格式应该不难;如果您有问题,请告诉我。

输出:

/* 1 */
{
    "_id" : {
        "stack" : "Java",
        "subStack" : "Spring"
    },
    "result" : [ 
        {
            "_id" : ObjectId("5aaf5b65f2046e3bf671328a"),
            "stack" : "Java",
            "subStack" : "Spring"
        }
    ]
}

/* 2 */
{
    "_id" : {
        "stack" : "webTechnology",
        "subStack" : "angular"
    },
    "result" : [ 
        {
            "_id" : ObjectId("5aaf5b65f2046e3bf6713285"),
            "stack" : "webTechnology",
            "subStack" : "angular"
        }, 
        {
            "_id" : ObjectId("5aaf5b65f2046e3bf6713287"),
            "stack" : "webTechnology",
            "subStack" : "angular"
        }
    ]
}

/* 3 */
{
    "_id" : {
        "stack" : "script",
        "subStack" : "python"
    },
    "result" : [ 
        {
            "_id" : ObjectId("5aaf5b65f2046e3bf6713288"),
            "stack" : "script",
            "subStack" : "python"
        }
    ]
}

/* 4 */
{
    "_id" : {
        "stack" : "webTechnology",
        "subStack" : "react"
    },
    "result" : [ 
        {
            "_id" : ObjectId("5aaf5b65f2046e3bf6713286"),
            "stack" : "webTechnology",
            "subStack" : "react"
        }
    ]
}

/* 5 */
{
    "_id" : {
        "stack" : "script",
        "subStack" : "javaScript"
    },
    "result" : [ 
        {
            "_id" : ObjectId("5aaf5b65f2046e3bf6713289"),
            "stack" : "script",
            "subStack" : "javaScript"
        }
    ]
}