使用ref填充对象数组,ref也是对象猫鼬的数组

时间:2018-12-10 11:51:55

标签: node.js express mongoose

var wordsSchema = new mongoose.Schema({
    words: [{
        w_id: Schema.Types.ObjectId,
        name: String,
        meaning: String,
        synonyms: String,
        sentence: String
    }]

})

var knownWordsSchema = new Schema({
kWords:[{
    type: Schema.Types.ObjectId,
    ref: "Words"
}]

})

我如何填充KnownWords,因为我需要填充的objectId位于对象的内部和数组中

例如:如果knownWords的ID为“ a”且单词为words: [{ w_id: a, name: "h", meaning: "h", synonyms: "h", sentence: "h" }]

我希望knownWords的对象为{ name: "h", meaning: "h", synonyms: "h", sentence: "h" }

**附言:我是一个初学者,所以我不知道如何用语言表达。**

1 个答案:

答案 0 :(得分:0)

KnownWordSchema的格式不正确。

get_foo

现在在您代码的某个位置:

let knownWordsSchema = new Schema({
kWords:[{
    words:{
    type: Schema.Types.ObjectId,
    ref: "Words"
    }
}]

populate方法将填充指定的字段。