如何在nodeJS中使用mongoose定义外键?
以下是我尝试制作的架构示例,其中id_def
需要引用def
表的主键id
var abcSchema = mongoose.Schema({
name: String,
id_def: Number // foreign key
});
提前致谢!
答案 0 :(得分:1)
试试这个:
var abcSchema = mongoose.Schema({
name: String,
_def: { type: mongoose.Schema.Types.ObjectId, ref: 'def' }
});
答案 1 :(得分:1)
var abcSchema = mongoose.Schema({
name: String,
desc: {
type: mongoose.Schema.Types.ObjectId,
ref: 'desc'
}
});