求关联外键的思路,ref能否关联多个模型?
发布于 8 年前 作者 varjay 3713 次浏览 来自 问答

例如我现在做一个回复功能,这个回复功能想整合到 movie模型和document模型(而不是两个回复功能重复开发),那么该怎么做呢?

var CommentSchema = new Schema({ movie: {type: ObjectId, ref: ‘Movie’}, //被评论的电影id,这里的ref能指定两个吗? from: {type: ObjectId, ref: ‘User’}, content: String })

2 回复
new Schema {
  targetType:
    type: String
    enum: ['Movie', 'Document']
  target:
    type: ObjectId
    refPath: 'targetType'
}

@ravenwang 厉害,我试试

回到顶部