Author Profile Altaf Shaikh
Altaf Shaikh

BE Engg | Python Dev | Content Writer | Founder of TeachMeBro | Blockchain Dev | Django

Creating Todo App Model

January 26th 2021   456

Creating Todo App Model
Creating Todo App Model

In this section, we will create Mongoose model for our Todo app. If you don't know what is Mongoose, how to create schema and model using it, refer this tutorial to know more.

So before connecting with the mongodb database we need to structure how we are going to model our data for todo. In our case which fields we will need to store the information about a todo task.

Task model

taskID - to uniquely identify the task

description - to store description about a task

completed - this will hold boolean value and it will help us to determine whether the task is completed or uncompleted

These fields are sufficient for a basic todo app to store information about a task, we can easily perform CRUD operation on this model.

So we have finalized the fields for our model, so now let's move ahead and create our mongoose schema model

Mongoose Schema Model

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB.

I know the definition would not have made much sense to you so let me explain it to you in simple words.

Mongodb is schema less database so to enforce definite schema on the database mongoose is used, along with the power of noSQL, it has in-built validation, we can define on our schemas.

To learn more about mongoose checkout this tutorial here.

Todo Model

Model is an object representation of data in the database. 

Install following packages:

npm install mongoose uniquid

We had installed mongoose and uniquid package to generate unique ids for taskID.

So, Let's create a file models/todo.js with the content:

// import mongoose
const mongoose = require("mongoose");

// reference to Schema from mongoose
const Schema = mongoose.Schema;

// creating and defining the schema
const todoSchema = new Schema(

  taskID: {

    type: String,

    required: true,


  description: {

    type: String,

    required: true,


  completed: {

    type: Boolean,

    default: false,




  timestamps: true,



// use the schema to create a model
var Todos = mongoose.model("Todo", todoSchema);

// export the Todos model to use inside other modules(js files)
module.exports = Todos;

So now our Todo model is ready we can now move on to make database connection with the mongodb for that we are going to use MongoDB Atlas.

In the next section, we will setup our MongoDB databse on Atlas and setup the database connection in Nodejs.