Thursday-Friday: Quiz: MongoDB – Todo Copy


For this assignment you will create the same todo app as the previous week but instead of storing the tasks in an array of todos now you will be moving to the real deal which is gonna be using database, to be more specific MongoDB and mongoose.


Step 1- project initialization :
  • create a directory to hold your application, and make that your working directory.

  • Start by creating your server same way of crud assignment :

    • initialize your app / install express / set up your routes so your final js code will look something like this :

And your package.json look like this :

Step 2 – Database

You need to create your database and for this assignment you will be using MongoDB Atlas … let’s create an account

– Create A MongoDB Atlas Account

The process is simple. Go ahead to the MongoDB Atlas and create a new account.

  • If it’s already done, you can log into your existing account.
  • Once you create your account or log in, click on the “Create a new cluster button from your dashboard.

  • A new page should appear, asking you for details for creating your new cluster.
  • Note that if you have already created a cluster, a second cluster will cost you money. If that’s your first time, then it’s free. Go ahead and click on the “Create Cluster” button.

– Create your database

The next step is to create your database. For that, you need to create a database user. On your dashboard, click on the “Database Access” button.

Define a username and a password you’ll remember and click the “Add User” button. Make sure the user has read and write access to your database.

– Whitelist your IP Address

Whitelisting your IP address is a necessary step in order to connect with your local project. For that, click the “Network Access” tab. You can whitelist your current IP address or allow access from everywhere. In this case, simply choose to whitelist your current IP address.

– Install mongoose :

Now you need to install Mongoose for interacting with your MongoDB Atlas database as well as any local database

$ npm i mongoose
– Connection with MongoDB Atlas

Now you connect your application with MongoDB Atlas. For that, you need to create a file called connection.js to establish the connection. Inside this file, you will have this code:

Let’s break this down:

  • Define an object with the connection parameters. This is useful for connecting to our MongoDB Atlas server.
  • Now you have a URI variable. This holds your MongoDB Atlas connection URL. I’ll show you where to get yours in a minute.
  • Next, you establish the connection between the server and MongoDB Atlas. This is done through the command mongoose.connect(). The connection is an asynchronous operation. So you handle it with a .then() if the connection is established or .catch() if there are issues in our connection.
  • Finally, you export our connection object using module.exports.
– How to get your MongoDB URI:

To get your URI, it’s simple. In your dashboard (MongoDB Atlas), click “Connect”, then “Connect to an application”.

This should pop up now:

You have a URI you can use in your connection.js file where <password> is a placeholder for your actual MongoDB database user password.

– Export your connection to app.js

After setting up your connection, you can import it in your server.js file:

const connection = require('./connection.js');

Now, run the app by opening your terminal and typing the command:

$ npm start

If everything goes well, you should have a message in your console saying “connected to database”

Step3 – CRUD Operations

Now you have your database & server set up, it’s time to create your Todos.

  • For that, you create a Todo model.
  • This Todo model is a blueprint that helps us create or read Todos from our database.

In your root folder, create a todoModel.js file. Your folder structure should look like this:

  • In your todoModel.js, lies your model for creating a new Todo to your Mongodb database. It should look like this:

Here you did the following things:

  • Create a schema with a title and a content
  • Set a post model that uses this schema
  • Export the todo to use it in your server.js file

Now you can use this Todo model when you are about to create or update your Todos.

Create Todo – post

Before doing any operation, you need to import your Model into the server.js file.

const todoModel = require("./todoModel.js)

The first part of the CRUD operations is the creation of a new todo. For that, you add a new route like that:

Let’s break it down:

  • When a post request is made to our server, it’s being parsed to retrieve the todo.
  • We then wrap the todo creation into a try…catch. This allows us to handle asynchronous operations as well as any errors that may happen.
  • Once the new todo is created using the todoModel, it should return it to the client as JSON data.
Reading or getting posts – get

To get all posts from your database, we define this route:

This is an asynchronous request, so you use a try…catch and prepend the async keyword to your callback function. This gives you an array including all your blog posts as JSON.

Update & Delete a Todo
  • Now and after understanding how to post and to get data from your database you need to complete the PUT and DELETE requests by yourself 👩🏻‍💻👨🏻‍💻
Step4 – GitHub Workflow:
  • Create a private repo with the same name as the quiz.
  • Push your solution, and add your instructor as a collaborator.
  • And finally, submit your pull request URL in the quiz submission box.