当object_id是字符串时,mongoose引用不起作用

时间:2017-09-12 07:10:41

标签: mongodb mongoose mongoose-schema mongoose-populate

我有两个mongo集合,一个用于部门,另一个用于用户

ProgressBar

现在我已经在部门集合中插入了一个文档,其中_id为' DEPT0001'但在用数据注册用户时

    var DeptSchema = new Schema({
name: {
    type: String,
    required: 'Department Name Missing.'
}},{
timestamps: true
});
DeptSchema.plugin(sequenceGenerator, {
field: '_id',
startAt: '0001',
prefix: 'DEPT'
});
module.exports = mongoose.model('departments', DeptSchema);

//User or staff schema definition
var UserSchema = new Schema({
name: {
    type: String,
    required: 'User Name Missing.'
},
role: {
    type: String,
    required: 'Role Missing.',
    enum: ['admin', 'sales']
},
passwordHash: String,
passwordSalt: String,
departmentId: {
    type: Schema.Types.ObjectId,
    required: 'Department Id Missing.',
    ref: 'departments'
}
},{
timestamps: true
});
UserSchema.plugin(sequenceGenerator, {
field: '_id',
startAt: '0001',
prefix: 'staff'
});
module.exports = mongoose.model('users', UserSchema);
邮递员

给出错误

    { name: 'rahul agarwal',
      username: 'rahul43',
      password: 'rev@888',
      departmentId: 'DEPT0001',
      role: 'admin' }

我的问题是我不应该使用自定义的_id字段作为参考,如果可行,那么为什么我不能引用它。

0 个答案:

没有答案