Skip to content
the poor coder

how to make complex schema in mongoose

Poor Man
Published
Reading time
2 min read

In Mongoose, you can create a complex schema by defining a schema with subdocuments and nesting schemas within them. To create a schema with subdocuments, you need to use the mongoose.Schema.Types.ObjectId type. This type allows you to store an array of objects with different fields. For example, to create a schema for a user object with a list of addresses, you could use the following code:

Text

const userSchema = new mongoose.Schema({
  name: { type: String, required: true},
  addresses: [{
    address1: { type: String, required: true },
    address2: { type: String, required: false },
    city: { type: String, required: true },
    state: { type: String, required: true },
    zip: { type: String, required: true }
  }]
});

This schema defines an object with a name field and an array of address objects. Each address object contains the fields address1, address2, city, state, and zip. You can also nest schemas within each other if you need to create a more complex schema. For example, you could create a schema for a user object with a list of orders like this:

Text

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  orders: [{
    orderId: { type: String, required: true },
    items: [{
      itemId: { type: String, required: true },
      name: { type: String, required: true },
      quantity: { type: Number, required: true },
      price: { type: Number, required: true }
    }]
  }]
});

This schema defines an object with a name field and an array of order objects. Each order object contains an orderId and an array of item objects. Each item object contains the fields itemId, name, quantity, and price. You can use the highlight.js library to provide syntax highlighting to your code. To do this, you need to include the highlight.js library in your HTML page and then call the hljs.initHighlightingOnLoad() method. Creating a complex schema with Mongoose can be done quickly and easily. By using the mongoose.Schema.Types.ObjectId type, you can create a schema with subdocuments and by nesting schemas within each other you can create even more complex schemas.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC