Database

[{_id:ObjectId("5a934e000102030405000000"),name:"john",sex:"male",hobbies:["football","tennis","swimming"]},{_id:ObjectId("5a934e000102030405000001"),name:"betty",sex:"female",hobbies:["football","tennis"]},{_id:ObjectId("5a934e000102030405000002"),name:"frank",sex:"male",hobbies:["football","tennis"]}]

Query

db.collection.aggregate([{$setWindowFields:{partitionBy:"$sex",output:{totalCount:{$count:{}}}}},{$unwind:"$hobbies"},{$group:{_id:{sex:"$sex",hobbies:"$hobbies"},count:{$sum:1},totalCount:{$first:"$totalCount"}}},{$group:{_id:"$_id.sex",hobbies:{$push:{k:"$_id.hobbies",v:"$count"}},total:{$first:"$totalCount"}}},{$set:{hobbies:{$arrayToObject:"$hobbies"},}}])

Result