首页 > 解决方案 > 如何在 Mongoose 中将一个模型引用到另一个模型?

问题描述

我知道如何在 Mongoose 中引用文档,但某些事情我不清楚。

基本上我到目前为止所知道的:

const Task = mongoose.model('task' , {

description : {
    type : String , 
    require : true , 
    trim : true 
} ,

completed : {
    type : Boolean ,
    default : false
} , 

owner : {
    type : mongoose.Schema.Types.ObjectId ,
    required : true , 
    ref : 'User'
}
})

但我不明白的是,mongoose 与用户模型的_id有什么关系,为什么其他一些字段没有相同的类型。让我更复杂地改写一下,如果我的所有者类型设置为String ,用户模型中可以有多个String类型的字段。那么我们如何引用它呢?

标签: node.jsmongodbmongoosemongoose-populate

解决方案


推荐阅读