DEV Community

Anuj Pancholi
Anuj Pancholi

Posted on

A "Nested Grouping" of sorts In MongoDb

Mongo Playground Link

I think this is the precise format you wanted.

The aggregation pipeline:

[
  {
    "$unwind": {
      "path": "$scores"
    }
  }
  {
    "$match": {
      "scores.type": "homework"
    }
  },
  {
    "$group": {
      "_id": {
        "class_id": "$class_id",
        "student_id": "$student_id"
      },
      "scores": {
        "$push": {
          "type": "$scores.type",
          "score": "$scores.score"
        }
      }

Top comments (0)