How can I find room by ID and make sure that the room has the current player in it?
My mongodb has a document of rooms which has players and a player is a user.
const RoomSchema = new Schema({
players: [{ type: Schema.Types.ObjectId, ref: "Player" }]
})
const PlayerSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: "User" }
})
const UserSchema = new Schema({
username: { type: String}
})
I want to find room where id === roomId and room has a player with user._id === userId
My query so far just finds one room by ID but I want to make sure that the room returned has the current user in as a player
RoomModel
.findOne({_id: roomId})
.populate({
path: 'players',
populate: {
path: 'user',
model: 'User',
select: ["_id", "username"]
}
})