如何使用mongoose定义外键?

时间:2014-08-09 02:11:30

标签: node.js mongodb mongoose

如何在nodeJS中使用mongoose定义外键?

以下是我尝试制作的架构示例,其中id_def需要引用def表的主键id

var abcSchema = mongoose.Schema({
    name: String,
    id_def: Number // foreign key
});

提前致谢!

2 个答案:

答案 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'
    }
});