0

I have datas in my mongoDB as follow

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

I need to return these data as response from Rest API in the following format, Categorized by stack and 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"
            }
        ]
    ]
]

Im using mongoTemplate to retrieve the data, which is returning in the List format

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

How can I get the result in the way which Im expecting?

1
  • Are you using groovy or java? Commented Mar 19, 2018 at 6:48

1 Answer 1

1

I inserted your data into a temp collection dataCollection and here is the query for grouping the data by stack and substack:

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

    {

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

              }

    }

])

It should not be hard to convert the query to mongotemplate format; let me know if you have issues.

Output:

/* 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"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have formed the query and it gives the result as expected. Thanks @dsharew. Can someone help me on how to send this through ResponseEntity from REST API, DBObject query = new BasicDBObject("$group", new BasicDBObject("_id", new BasicDBObject("stack", "$stack").append("subStack", "$subStack")) .append("result", new BasicDBObject("$push", "$$ROOT"))); AggregationOutput output = mongoTemplate.getCollection("contributionInfoBean").aggregate(query);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.