Mongo Playground

Configuration

[{name:"john",sex:"male",hobbies:["football","tennis","swimming"]},{name:"betty",sex:"female",hobbies:["football","tennis"]},{name:"frank",sex:"male",hobbies:["football","tennis"]}]

Query

db.collection.aggregate([{$set:{o:{$function:{"body":"function (x) {x._id=new ObjectId(); return x}","args":[{_id:1}],"lang":"js"}}}},{$unwind:"$hobbies"},{$group:{_id:{sex:"$sex",hobbies:"$hobbies"},count:{$sum:1},totalIds:{$addToSet:"$o._id"}}},{$group:{_id:"$_id.sex",hobbies:{$push:{k:"$_id.hobbies",v:"$count"}},totalIds:{$push:"$totalIds"}}},{$set:{hobbies:{$arrayToObject:"$hobbies"},totalIds:{$reduce:{input:"$totalIds",initialValue:[],in:{$concatArrays:["$$value","$$this"]}}}}},{$set:{count:{$size:{$setIntersection:"$totalIds"}},totalIds:"$$REMOVE"}}])

Result