Database

db={"Departments":[{"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.Departments.aggregate([{$match:{$and:[{"subject.failed":{$eq:"true"}},{"section":{$eq:"A"}}]}},{$group:{_id:{"sectionType":"$sectionType"},"COUNT(*)":{$sum:1}}},{$project:{"totalFailed":"$COUNT(*)","sectionType":"$_id.sectionType","_id":0}},{"$lookup":{"from":"Departments","let":{st:"$sectionType"},"pipeline":[{$match:{$expr:{$and:[{$eq:["$sectionType","$$st"]},{$eq:["$section","A"]}]}}},{$group:{_id:null,"COUNT(*)":{$sum:1}}},{$project:{"totalCount":"$COUNT(*)","_id":0}}],"as":"query2"}}])

Result