Define a Mongoose schema for REST API routes

This exercise walks through defining Mongoose schema for the REST API routes that were created in the previous exercise.


Define a Mongoose schema and create a Model for the customer resource


This is a continuation of the exercise to create REST API for the customer’s resource. In the last exercise, you built the structure for your API in express.js. In this exercise, you will be creating a Mongoose schema that will serve as a guideline to insert documents in the MongoDB database in a later exercise. We will be using MongoDB as our database to store the customer data of our application.

Express apps can use any database such as MySQL, SQLite, PostgreSQL, and MongoDB that are supported by Node.js, and you can do these exercises using the database of your choice. When choosing a database you should consider important factors such as performance, cost, the learning curve for the team, backups, etc. We are, however, using MongoDB which is a NoSQL database. Using Object Data Model (“ODM”) is a way of interacting with the data with a database. And Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js.

The ODM (Mongoose) and database (MongoDB) combination is popular among JavaScript developers because the document storage and query system looks like JSON and thus makes a good choice for learning backend development using JavaScript. You don’t need to know MongoDB in order to get started with Mongoose


Create a customer schema that has the following properties:

- Customer schema -
first_name : string [required]
last_name : string
email : string
phone : number [required]
verified_email : boolean
address : map

Create and export a model called Customer from the customerSchema


Install mongoose and require mongoose

npm install mongoose --save

const mongoose = require('mongoose')

Assign Mongoose Schema to a variable

const Schema = mongoose.Schema

A schema (`productSchema`) can be created as follows:

const productSchema = new Schema({
  title: { type: String, required: true },
  description: { type: String },
  created_at: { type: Date },
  product_type: { type: String },
  tags: { type: String },
  vendor: { type: String },

required: true property is used for validation

Model for productSchema can be created as:

const Product = mongoose.model('Product', productSchema);

Use module.exports to export the model.

module.exports = Product;


Edit define_mongoose_schema

Solution Walkthrough


MongoDB is a NoSQL database that stores data records (documents) for use by an application. And Mongoose is an Object Data Modeling (ODM) library that adds a layer of methods that helps to manage relationships between data, provides schema. It helps to create Models and Schemas easily.


Collections A collection is a group of records stored in MongoDB. It is equivalent to tables in relational databases.

Documents ‘Documents’ are data records and they are equivalent to rows of data in SQL.

Fields ‘Fields’ or attributes are similar to columns in a SQL table.

Schema A Mongoose schema defines the structure of the document

Models Mongoose model is a higher-order constructor that takes a schema and provides an interface to the database for creating, querying, updating, deleting records,


Let’s start by installing Mongoose

npm i mongoose

Next, create a new folder models in the root directory and then create a new file customer.js. This file will represent the “Customer” model.

Defining schema:

Models are defined using the Schema. The Schema will define the fields that will be stored in each document along with their validation requirements and default values.

Now, require Mongoose in the newly created customer.js file and assign the Mongoose schema to a variable.

//Require mongoose
const mongoose = require("mongoose");
//Define schema
const Schema = mongoose.Schema;

For this exercise, we have to add the following fields to our schema:

  • first_name : string [required]
  • last_name: string
  • email: string
  • phone : number [required]
  • verified_email: boolean
  • address: map

These are the fields that will be present in each document or record that will be added to our collection in MongoDB.

Use the Schema constructor (defined above) to create a new schema instance, and add the fields (eg: name, email id) in the constructor’s object parameter.

const customerSchema = new Schema({
  first_name: { type: String, required: true },
  last_name: { type: String },
  email: { type: String },
  phone: { type: Number },
  verified_email: { type: Boolean },
  address: { type: Map },

type: String indicates that we expect the first_name property to be of type String.

required: true property is used for validation

Creating Model

Models are created from schemas using the mongoose.model(arg1, arg2) .method.

//Create model from Schema
const Customer = mongoose.model("Customer", customerSchema);

The first argument is the singular name of the collection that will be created for the model. In this example, Mongoose will create the database collection for the Customer model. The second argument is the schema you want to use for creating the model. customerSchema in this example.

Next export the model, so it can be used to create, update, or delete records, and run queries.

module.exports = Customer;