Group By Variable in Mongo Aggregate

Viewed 551

I have a mongo aggregation which currently ends in this project:

      {
        $project: {
          _id: 1,
          name: 1,
          subject_id: '$subject.subject',
          groups: '$groups'
        }
      }

It creates this array of tests:

0:
_id: 1
groups: (27)
name: "Year 1 Maths Paper 1"
subject_id: "111"
1:
_id:2
groups: (27)
name: "Year 1 Maths Paper 2"
subject_id: "111"

However, I want to group the data by the subject_id. So each subject will be an array with tests related to it inside. Does anyone know how to do this?

1 Answers
Related