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":{"$setDifference":["$attendees",["$$a"]]}},"as":"a"}}}},{"$project":{"_id":0,"a":1}},{"$unwind":{"path":"$a"}},{"$replaceRoot":{"newRoot":"$a"}},{"$unwind":{"path":"$togetherWith","preserveNullAndEmptyArrays":true}},{"$set":{"togetherWith":{"$ifNull":["$togetherWith",null]}}},{"$group":{"_id":"$attendee","howManyEvents":{"$addToSet":"$event"},"together":{"$push":{"togetherWith":"$togetherWith","howManyTimes":1}}}},{"$set":{"attendee":"$_id"}},{"$project":{"_id":0}},{"$set":{"howManyEvents":{"$size":"$howManyEvents"}}},{"$unwind":{"path":"$together"}},{"$replaceRoot":{"newRoot":{"$mergeObjects":["$together","$$ROOT"]}}},{"$project":{"together":0}},{"$group":{"_id":{"attendee":"$attendee","togetherWith":"$togetherWith"},"howManyEvents":{"$first":"$howManyEvents"},"howManyTimes":{"$sum":"$howManyTimes"}}},{"$replaceRoot":{"newRoot":{"$mergeObjects":["$_id","$$ROOT"]}}},{"$project":{"_id":0}}])

Result