Database

[{"event":"event_1","attendees":["A","B","C"]},{"event":"event_2","attendees":["A","B"]},{"event":"event_3","attendees":["A"]},{"event":"event_4","attendees":["Z"]}]

Query

db.collection.aggregate([{"$set":{"a":{"$map":{"input":"$attendees","in":{"attendee":"$$a","event":"$event","togetherWith":{"$filter":{"input":{"$map":{"input":"$attendees","in":{"$cond":[{"$ne":["$$a","$$a1"]},{"togetherWith":"$$a1","howManyTimes":1},null]},"as":"a1"}},"cond":{"$ne":["$$f",null]},"as":"f"}}},"as":"a"}}}},{"$project":{"_id":0,"a":1}},{"$unwind":{"path":"$a"}},{"$unwind":{"path":"$a.togetherWith","preserveNullAndEmptyArrays":true}},{"$project":{"attendee":"$a.attendee","togetherWith":"$a.togetherWith.togetherWith","howManyTimes":"$a.togetherWith.howManyTimes","event":"$a.event"}},{"$facet":{"a":[{"$group":{"_id":"$attendee","howManyEvents":{"$addToSet":"$event"}}},{"$set":{"attendee":"$_id"}},{"$project":{"_id":0}},{"$set":{"howManyEvents":{"$size":"$howManyEvents"}}}],"b":[{"$group":{"_id":{"attendee":"$attendee","togetherWith":"$togetherWith"},"howManyTimes":{"$sum":"$howManyTimes"}}},{"$replaceRoot":{"newRoot":{"$mergeObjects":["$_id","$$ROOT"]}}},{"$project":{"_id":0}}]}},{"$set":{"b":{"$map":{"input":"$b","in":{"$let":{"vars":{"howManyEvents":{"$arrayElemAt":[{"$filter":{"input":"$a","cond":{"$eq":["$$m.attendee","$$m1.attendee"]},"as":"m1"}},0]}},"in":{"$mergeObjects":["$$m",{"howManyEvents":"$$howManyEvents.howManyEvents"}]}}},"as":"m"}}}},{"$unwind":{"path":"$b"}},{"$replaceRoot":{"newRoot":"$b"}}])

Result