如何通过 ID 找到房间并确保房间中有当前玩家?
我的 mongodb 有一个房间文档,其中包含玩家,玩家是用户。
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}
})
我想找到 id === roomId 的房间,并且房间有一个带有 user._id === userId 的玩家
到目前为止,我的查询仅通过 ID 找到一个房间,但我想确保返回的房间有当前用户作为玩家
RoomModel
.findOne({_id: roomId})
.populate({
path: 'players',
populate: {
path: 'user',
model: 'User',
select: ["_id", "username"]
}
})