Home > Enterprise >  Mongoose Nested Schema - required fields inside array field
Mongoose Nested Schema - required fields inside array field

Time:04-20

I have following mongoose schema -

  const UserSchema = new mongoose.Schema({
        name: {type: String, required: true, trim: true},
        email: {type: String, required: true, trim: true},
        password: {type: String, required: true, trim: true},
        addresses: [{
            type: {type: String, required: true, trim: true},
            pinCode: {type: String, required: true, trim: true},
            city: {type: String, required: true, trim: true},
            state: {type: String, required: true, trim: true},
            landmark: {type: String, required: false, trim: true},
        }]
     })

only name, email and password are required for registration. User can have more than one address , and each address has some required fields like in enter image description here

Hope this will help you. For more information please visit this mongoose documentation link.

  • Related