Database
db={"collectiona":[{"_id":"6173ddf33ed09368a094e68a","title":"a"},{"_id":"61wefdf33ed09368a094e6dc","title":"b"},{"_id":"61wefdfewf09368a094ezzz","title":"c"},],"collectionb":[{"_id":"6173ddf33ed0wef368a094zq","collectionaID":"6173ddf33ed09368a094e68a","data":[{"userID":"123","visibility":false,"response":false},{"userID":"2345","visibility":true,"response":true}]},{"_id":"6173ddf33ed09368awef4e68g","collectionaID":"61wefdf33ed09368a094e6dc","data":[{"userID":"5678","visibility":false,"response":false},{"userID":"674","visibility":true,"response":false}]},{"_id":"6173ddf33ed093w8awef4e68g","collectionaID":"61wefdfewf09368a094ezzz","data":[{"userID":"123","visibility":true,"response":true},]}]}
Query
db.collectiona.aggregate([{"$lookup":{"from":"collectionb",let:{aid:"$_id"},"pipeline":[{$unwind:"$data"},{$match:{$expr:{$and:[{$eq:["$data.userID","2345"]},{$eq:["$collectionaID","$$aid"]}]}}}],"as":"collB"}},{$match:{"collB.data.response":{$ne:true}}},{"$unwind":{path:"$collB",preserveNullAndEmptyArrays:true}},{"$addFields":{"sortrank":{"$cond":{"if":{$eq:["$collB.data.visibility",false]},"then":1,"else":{"$cond":{"if":{$eq:["$collB.collectionaID","$_id"]},"then":3,"else":2}}}}}},{$sort:{sortrank:1}},{$project:{collB:false,sortrank:false}}])