Mongo Playground

Configuration

[{"task_id":"as4d2rds5","url":"https:example1.com","organization":"Avengers","val":"null"},{"task_id":"rfre43fed","url":"https:example2.com","organization":"Avengers","val":"valid"},{"task_id":"uyje3dsxs","url":"https:example3.com","organization":"Metro","val":"valid"},{"task_id":"ghs563vt6","url":"https:example4.com","organization":"Avengers","val":"invalid"},{"task_id":"erf6egy64","url":"https:example5.com","organization":"Metro","val":"null"}]

Query

db.collection.aggregate([{"$group":{"_id":{k:"$organization",v:"$val"},"cnt":{$sum:1}}},{$project:{_id:0,k:"$_id.k",o:{k:"$_id.v",v:"$cnt"}}},{$group:{_id:"$k",v:{$push:"$o"}}},{$addFields:{v:{"$mergeObjects":[{"null":0,valid:0,invalid:0},{"$arrayToObject":"$v"}]}}},{$project:{_id:0,new:[{k:"$_id",v:"$v"}]}},{"$addFields":{"new":{"$arrayToObject":"$new"}}},{"$replaceRoot":{"newRoot":"$new"}}])/** [* {"Avengers":{"valid":1,"null":1,"invalid":1}},* {"Metro": {"valid":1,"null":1,"invalid":0}}* ]**/

Result