Q: MongoDB Query Optimization - Review Good Day !
I have the below set of collections
[
  {
    "packageName": "pack1",
    "className": "class1",
    "status": "passed"
  },
  {
    "packageName": "pack1",
    "className": "class1",
    "status": "ignored"
  },
  {
    "packageName": "pack1",
    "className": "class2",
    "status": "failed"
  },
  {
    "packageName": "pack2",
    "className": "class1",
    "status": "passed"
  },
  {
    "packageName": "pack2",
    "className": "class1",
    "status": "passed"
  },
  
]

In order to group by package and then count the pass/fail/ignore count and calculate the class count, I've created the below query
db.collection.aggregate([
  {
    "$group": {
      "_id": "$packageName",
      "classes": {
        "$addToSet": "$className"
      },
      "passCount": {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "$status",
                "passed"
              ]
            },
            1,
            0
          ]
        }
      },
      "failCount": {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "$status",
                "failed"
              ]
            },
            1,
            0
          ]
        }
      },
      "ignoredCount": {
        "$sum": {
          "$cond": [
            {
              "$eq": [
                "$status",
                "ignored"
              ]
            },
            1,
            0
          ]
        }
      },
      
    }
  },
  {
    "$project": {
      "packageName": "$_id",
      "classCount": {
        "$size": "$classes"
      },
      "passCount": 1,
      "failCount": 1,
      "ignoredCount": 1
    }
  }
])

to get the following output
[
  {
    "_id": "pack1",
    "classCount": 2,
    "failCount": 1,
    "ignoredCount": 1,
    "packageName": "pack1",
    "passCount": 1
  },
  {
    "_id": "pack2",
    "classCount": 1,
    "failCount": 0,
    "ignoredCount": 0,
    "packageName": "pack2",
    "passCount": 2
  }
]

Being a beginner in MongoDB, I'm not sure if it's optimized. I've been reading the docs and this is what I've come up. It would be much appreciated if you could help me with optimizing this query, provide your reviews.
Thanks for your time !
