Database

db={"users":[{"_id":{"$oid":"627b519f73b2bd3375f5a7a5"},"userProgress":{"meditationsPracticed":[{"timestamp":"11.5.2022, 9:03:34","id":"5ef2ff0af1a23752be00651f"},{"timestamp":"11.5.2022, 12:46:03","id":"5eca520c10fe0480d350c9a4"}]}}],"meditations":[{"_id":{"$oid":"5eca520c10fe0480d350c9ac"},"name":"Sleep Well","duration":{"$numberInt":"250"}}]}

Query

db.users.aggregate([{"$match":{"_id":{"$oid":"627b519f73b2bd3375f5a7a5"}}},{"$unwind":"$userProgress.meditationsPracticed"},{"$group":{"_id":{"$toObjectId":"$userProgress.meditationsPracticed.id"},"count":{"$sum":1}}},{"$setWindowFields":{"partitionBy":"$_id","sortBy":{"count":-1},"output":{"rank":{"$rank":{}}}}},{"$match":{"rank":1}},{"$lookup":{"from":"meditations","localField":"_id","foreignField":"_id","as":"meditationsLookup"}},{"$unwind":{"path":"$meditationsLookup","preserveNullAndEmptyArrays":true}}])

Result