Reputation: 203
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
Upvotes: 1
Views: 64
Reputation: 1815
There isn't any validation by default in the schema for any Reference Object Id
What you can do is set up one synchronous validation and make a findOne
call in it to validate.
Upvotes: 1