To add nested objects in Node Mongoose schemas, we set the type
property to another schema.
For instance, we write
const AddressSchema = mongoose.Schema({
city: String,
street: String,
houseNumber: String,
});
const ContactInfoSchema = mongoose.Schema({
tel: [Number],
email: [String],
address: {
type: AddressSchema,
required: true,
},
});
const CustomerSchema = mongoose.Schema({
firstName: String,
lastName: String,
company: String,
connectInfo: ContactInfoSchema,
});
to create the CustomerSchema
that has the connectInfo
property set to the ContactInfoSchema
.
And the ContactInfoSchema
has the address
property set to the AddressSchema
as its type.