Database

[{"_id":ObjectId("5a934e000102030405000000"),"emp_name":"John","status":1,"type":"a1"},{"_id":ObjectId("5a934e000102030405000001"),"emp_name":"John","status":0,"type":"a1"},{"_id":ObjectId("5a934e000102030405000002"),"emp_name":"Jane","status":0,"type":"a1"},{"_id":ObjectId("5a934e000102030405000003"),"emp_name":"John","status":1,"type":"a2"},{"_id":ObjectId("5a934e000102030405000004"),"emp_name":"Jane","status":0,"type":"a2"},{"_id":ObjectId("5a934e000102030405000005"),"emp_name":"Bob","status":1,"type":"a1"},{"_id":ObjectId("5a934e000102030405000006"),"emp_name":"Bob","status":0,"type":"a2"},{"_id":ObjectId("5a934e000102030405000007"),"emp_name":"Bob","status":1,"type":"a2"},{"_id":ObjectId("5a934e000102030405000008"),"emp_name":"Alice","status":1,"type":"a2"},{"_id":ObjectId("5a934e000102030405000009"),"emp_name":"Charles","status":1,"type":"a1"}]

Query

db.collection.aggregate([{$match:{type:{$in:["a1","a2"]}}},{$facet:{a1:[{$match:{type:"a1"}},{$project:{_id:0,emp_name:1}}],a2:[{$match:{type:"a2"}}]}},{$unwind:"$a2"},/** $replaceWith is actually not needed, just added as comsmetic and for better understanding*/{$replaceWith:{$mergeObjects:["$a2",{a1:{$setUnion:"$a1.emp_name"}}]}},{$match:{$expr:{$in:["$emp_name","$a1"]}}},{$group:{_id:"$emp_name",count:{$count:{}}}},{$project:{_id:0,type:"a2",count:1,empName:"$_id"}}])

Result