首页 > 解决方案 > Mongoose:加入操作,无法填充

问题描述

我目前正在为我的一个项目而苦苦挣扎。

我有一个名为“游戏”的集合和一个“参与”。当用户登录时,他/她应该能够看到游戏和个人参与状态。因此,我想加入这两个集合但我不能使用 .populate() 因为我无法在 Games 集合中输入必要的参与 ID,因为我当时没有参与 ID我创建一个游戏。(所以我需要保存参与,记住创建的 ID 并在之后将这个 ID 插入游戏集合中)反过来将是一个很好的解决方案(在参与中填充游戏)但最初,这些集合中没有参与,直到用户单击“参与”或“不参与”。


基本上我需要这样的 SQL 查询:

select * from games g, participation p where g.gamesId = p.gamesId AND p.username = [...]

有没有可能实现这一目标?否则,我需要将每次参与保存为“游戏”,其中包含相关用户名和他/她的参与状态。根本不喜欢这个解决方案。

提前致谢!

如果有帮助:这是我的两个系列:

游戏:

var gameSchema = mongoose.Schema(
{
    isHome: { type: Boolean, required: true },
    time: { type: String, required: true, max: 100 },
    uzeit: { type: String, required: true, max: 100 },
    clubId: { type: mongoose.Types.ObjectId, required: true },
    enemy: { type: String, required: true, max: 100 },
    place: { type: String, required: true, max: 100 },
    (participation: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Teilnahme' }])
});

参与:

var participationSchema = mongoose.Schema(
    {
        playerAvailable: { type: Boolean, required: true },
        clubId: { type: mongoose.Types.ObjectId, required: true },
        gameId: { type: mongoose.Types.ObjectId, required: true, ref: 'Game' },
        memberName: { type: String, required: true },
    }
);

标签: javascriptnode.jsmongoosebackendpopulate

解决方案


推荐阅读