使用ref填充对象数组,ref也是一个对象mongoose数组

loading...


0
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"
}]

})

How do i populate KnownWords, as the objectId i need to populate it is inside and array of object

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

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

** P.s:我是一名初学者,所以我不知道该怎么说 . **

loading...

1回答

  • 0

    KnownWordSchema格式不正确 .

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

    现在代码中的某个地方:

    KnownWords.findOne({words:req.params.w_id})
    .populate('words',['name','meaning','synonyms','sentence'])
    

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

评论

暂时没有评论!