简体   繁体   中英

How do I refer the id of schema in mongoose

 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM