How do I refer the id of schema in mongoose

时间:2017-10-08 07:55:33

标签: javascript node.js mongodb

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const CollageSchema = new Schema({
  collage_name: {
    type: String,
    required: [true, 'Name field is required']
  },
  university_id: {
    type: [{
      type: Schema.Types.ObjectId,
      ref: 'university'
    }]
  },
  type: {
    type: String,
    enum: ['autonomous', 'private'],
    required: [true, 'type field is required']
  }
});

const Collage = mongoose.model('collage', CollageSchema);
module.exports = Collage;

I have referenced _id of UniversitySchema in CollageSchema, but it will be taking any university_id that will not present in university table. Please help me. Thank you

1 个答案:

答案 0 :(得分:1)

默认情况下,任何参考对象ID的架构中都没有任何验证 您可以做的是设置一个同步验证并在其中进行findOne调用以进行验证。