Database
[{id:1,name:"John",hobbies:[{id:1,name:"football"},{id:4,name:"baseball"}]},{id:2,name:"Peter",hobbies:[{id:2,name:"basketball"}]},{id:3,name:"Sarah",hobbies:[{id:1,name:"football"},{id:3,name:"tennis"}],},{id:4,name:"Jennifer",hobbies:[{id:4,name:"baseball"}],}]
Query
db.collection.aggregate([{$match:{id:1}},{$limit:1},{$lookup:{from:"people",let:{/** Map and array of hobby ids to use in the pipeline* Result: [1, 4] (for John)*/personHobbyIds:{$map:{input:"$hobbies",as:"hobby",in:"$$hobby.id"}}},as:"peopleWithSimilarHobbies",pipeline:[/** For each person, create an array with their hobby ids* [2] (for Peter), [1, 3] (for Sarah), [1, 4] (for Jennifer) ...*/{$addFields:{peopleHobbyIds:{$map:{input:"$hobbies",as:"hobby",in:"$$hobby.id"}}}},/** Attempt to match $$personHobbyIds from the let stage with* the $peopleHobbyIds assigned from $addFields* !!! This doesn't work !!!*/{$match:{$expr:{$in:["$$personHobbyIds","$peopleHobbyIds"]}}}]}}])