Вы можете использовать структуру агрегации mongodb для этой задачи.
Playground
const result = await RoomModel.aggregate([
{
$match: {
_id: "1", // match by room id
},
},
{
$lookup: {
from: "players", // must be physical collection name, check if different
localField: "players",
foreignField: "_id",
as: "players",
},
},
{
$unwind: "$players",
},
{
$match: {
"players.user": "100", //match by user id
},
},
{
$lookup: {
from: "users",
localField: "players.user",
foreignField: "_id",
as: "user"
}
}
]);
if (result.length > 0) {
console.log("found"); //todo: add your logic when found
} else {
console.log("not found"); //todo: add your logic when not found
}
Это даст такой результат, когда пользователь найдет, вам может понадобиться некоторые преобразования.
[
{
"_id": "1",
"players": {
"_id": "10",
"user": "100"
},
"user": [
{
"_id": "100",
"username": "user1"
}
]
}
]