Database

db={users:[{_id:"abc",name:"abc",group:1},{_id:"xyz",name:"xyyy",group:1},{_id:"123",name:"yyy",group:1},{_id:"rrr",name:"tttt",group:1},{_id:"eee",name:"uuu",group:1}],partnership:[{_id:"abc_123",fromUser:"abc",toUser:"123"},{_id:"eee_rrr",fromUser:"eee",toUser:"rrr"},{_id:"rrr_abc",fromUser:"rrr",toUser:"abc"},{_id:"abc_rrr",fromUser:"abc",toUser:"rrr"},{_id:"xyz_rrr",fromUser:"xyz",toUser:"rrr"},{_id:"rrr_eee",fromUser:"rrr",toUser:"eee"},],locations:[{_id:"123",location:{type:"Point",coordinates:[54.23,67.12]},calculatedDist:13},{_id:"rrr",location:{type:"Point",coordinates:[51.23,64.12]},calculatedDist:14},{_id:"eee",location:{type:"Point",coordinates:[55.23,62.12]},calculatedDist:143},{_id:"abc",location:{type:"Point",coordinates:[53.23,67.12]},calculatedDist:112},]}

Query

db.locations.aggregate([/** $geoNear*/{$lookup:{from:"partnership",let:{user_id:"$_id"},pipeline:[{$match:{$expr:{$or:[{$and:[{$eq:["$fromUser","rrr"]},{$eq:["$toUser","$$user_id"]}]},{$and:[{$eq:["$toUser","rrr"]},{$eq:["$fromUser","$$user_id"]}]},]}}},],as:"valid"}},{$match:{"valid.0":{$exists:true}}},{$lookup:{from:"users",localField:"_id",foreignField:"_id",as:"user"}},{$project:{user:{$first:"$user"},calculatedDist:1}},{$sort:{calculatedDist:1}},{$group:{_id:0,users:{$push:"$user"},count:{$sum:1}}}])

Result