JavaScript Answers

How to add nested objects in Node Mongoose schemas?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.