填充JSON对象Mongoose

问题描述 投票:0回答:1

我想在JSON对象中填充,但它不起作用。这是代码。当我记录Group变量的响应时,我仍然得到的响应是objectId

人口

var Groups = await GroupModel.find({ownerUser : user.battletag}).populate([
                {
                    path: "selectedInstance",
                    model: "Instances"
                },
                {
                    path: "createdAs",
                    model: "Characters",
                    populate: {
                        path: "race",
                        model: "Races"
                    }
                },
                {
                    path: "characters",
                    populate: {
                        path: "character",
                        model: "Characters"
                    }
                }
            ]).exec();

模式

var GroupSchema = new Schema({
    createdAs: {type: Schema.Types.ObjectId, ref: 'Characters'},
    description: String,
    minimumItemLevel: Number,
    voiceChat: String,
    completedAchievements: [{ type: Schema.Types.ObjectId, ref: 'Achievements' }],
    selectedInstance: { type: Schema.Types.ObjectId, ref: 'Instances' },
    instanceDifficulty: String,
    private: Boolean,
    characters: [{character: { type: Schema.Types.ObjectId, ref: 'Characters' }, role: String}],
    queue: [{ type: Schema.Types.ObjectId, ref: 'Characters' }],
    ownerUser: String,
    groupSignature: String,
    status: {type: String, default: "Open"},
    maxPlayers: Number,
    minPlayers: Number,
    expansion: String,
    createDate: {type: Date, default: Date.now}
});
node.js mongodb mongoose schema population
1个回答
0
投票

我找到了问题的解决方案,现在它可以工作了!

 var Groups = await GroupModel.find({ownerUser : user.battletag}).populate([
            {
                path: "selectedInstance",
                model: "Instances"
            },
            {
                path: "createdAs",
                model: "Characters",
                populate: {
                    path: "race",
                    model: "Races"
                }
            },
            {
                path: "characters.character",
                model: "Characters"
            }
        ]).exec();
        return Groups
© www.soinside.com 2019 - 2024. All rights reserved.