Database
db={users:[{_id:"abc",name:"abc",group:1,location:{type:"Point",coordinates:[53.23,67.12]},calculatedDist:112},{_id:"xyz",name:"xyyy",group:1,location:{type:"Point",coordinates:[54.23,67.12]},calculatedDist:13},{_id:"123",name:"yyy",group:1,location:{type:"Point",coordinates:[54.23,67.12]},calculatedDist:13},{_id:"rrr",name:"tttt",group:1,location:{type:"Point",coordinates:[51.23,64.12]},calculatedDist:14},{_id:"mmm",name:"mmmm",group:1,location:{type:"Point",coordinates:[51.23,64.12]},calculatedDist:14},{_id:"eee",name:"uuu",group:1,location:{type:"Point",coordinates:[55.23,62.12]},calculatedDist:143}],partnership:[{_id:"xyz_rrr",users:["xyz","rrr"]},{_id:"rrr_eee",users:["rrr","eee"]},{_id:"eee_rrr",users:["eee","rrr"]},]}
Query
db.users.aggregate([{$lookup:{from:"partnership",let:{user_id:"$_id"},pipeline:[{$match:{$expr:{$eq:["rrr",{$arrayElemAt:["$users",1]}]}}}],as:"valid"}},{$match:{"valid.0":{$exists:true},location:{$exists:true}}},{$sort:{calculatedDist:1}},{$unset:["location","calculatedDist","valid"]},{$group:{_id:0,users:{$push:"$$ROOT"},count:{$sum:1}}}])