Database
[{"section":"A","sectionType":"typeA","subject":[{"failed":"true","subject_name":"Name A","subject_staff_count":"1245","subject_id":"a1111"},{"failed":"true","subject_name":"Name B","subject_staff_count":"2222","subject_id":"a2222"}]},{"section":"A","sectionType":"typeA","subject":[{"failed":"true","subject_name":"Name B","subject_staff_count":"3333","subject_id":"a331"},{"failed":"true","subject_name":"Name C","subject_staff_count":"4444","subject_id":"a44422"}]},{"section":"A","sectionType":"typeA","subject":[]}]
Query
db.collection.aggregate([{$match:{section:"A"}},{$set:{failed:{$ifNull:[{$first:"$subject.failed"},"false"]}}},{$setWindowFields:{partitionBy:{sectionType:"$sectionType",failed:"$failed"},output:{count_failed:{$sum:1}}}},{$setWindowFields:{partitionBy:"$sectionType",output:{count_total:{$sum:1}}}},{$match:{failed:"true"}},{$group:{_id:{sectionType:"$sectionType",totalCount:"$count_failed",totalFailed:"$count_total"}}}])