Get data from a collection as single object where name match and marge other properties in an object of array

Hi there,
I want some help to get data from a collection. For example, the collection has the following documents:
{ _id: 1, Name: ab, month: JAN, achieved_score: 20%}
{ _id: 2, Name: ab, month: FEB, achieved_score: 40%}
{ _id: 3, Name: ab, month: MAR, achieved_score: 50%}
{ _id: 4, Name: cd, month: JAN, achieved_score: 50%}
{ _id: 5, Name: cd, month: FEB, achieved_score: 70%}
{ _id: 6, Name: cd, month: MAR, achieved_score: 60%}
{ _id: 7, Name: ef, month: FEB, achieved_score: 30%}
{ _id: 8, Name: ef, month: MAR, achieved_score: 40%}

now I want to get data in a single object where the name match and month and achieved_score marge into an object of an array as following:

[
{Name:ab, [{month: JAN, achieved_score: 20%},{month: FEB, achieved_score: 40%},{month: MAR, achieved_score: 50%}]},
{Name:cd, [{month: JAN, achieved_score: 50%},{month: FEB, achieved_score: 70%},{month: MAR, achieved_score: 60%}]},
{Name:ef, [{month: FEB, achieved_score: 30%},{month: MAR, achieved_score: 40%}]},
]

Please help me to solve this query by using mongoose
Thanks.

Hi,

You can do it with Aggregation Framework and $group stage:

db.collection.aggregate([
  {
    "$group": {
      "_id": "$Name",
      "values": {
        "$addToSet": {
          "month": "$month",
          "achieved_score": "$achieved_score"
        }
      }
    }
  }
])

Working example