Skip to content

Mongo DB Setup

We will need to setup our Mongo Database before we start working on our REST API.

Mongo DB can be setup on your local machine, but for this tutorial, we will setup it on the Cloud. There are a lot of providers that offer free space for Mongo DB, we will use the official one called MongoDB Atlas (https://www.mongodb.com/atlas/database).

First thing you should do is to visit the link above and click button in "Try Free" in the top-right corner.

Mongo DB Try Free
Mongo DB Try Free

Then you will be asked to Register or Login. If you do not have account, please make one and login or if you do, just login.

MongoDB Atlas Login
MongoDB Atlas Login

After you successfully login, you will be asked to accept Privacy Policy. Do that. Then you will be asked some question about the project you will be using the new database for, you can define it like this:

MongoDB Porject Description
MongoDB Porject Description

And click "Finish".

After this you will be asked which plan you want to use, for this project we will go with "Free" one. Select it and click "Create".

MongoDB Plan
MongoDB Plan

This will prompt you the next screen like:

MongoDB Create Cluster
MongoDB Create Cluster

For our purposes, we can leave everything as it is and click "Create Cluster".

After Cluster is created, you will be asked to setup it. Something like this:

MongoDB Setup
MongoDB Setup

First thing we will need do, we will have to create our database user. Let's make a new test_user with some password like Test12345. (You should not share your passwords with anyone for real projects, this is a demo project so we can)

MongoDB User
MongoDB User

When our user is created, we want to select that we want connect from our local environment and to allow our IP to connect to it. You can do that by selecting "My Local Environment" card and clicking "Add My Current IP Address" button to whitelist your IP.

MongoDB Local Environment
MongoDB Local Environment

Then, you should be able to see your IP listed there and we are ready to go. You can click "Finish and Close" to save changes.

After that, a popup will appear that confirms that changes were applied and you will see the button "Go to Databases". Click that button to see the list of all currently available database we can use.

There, you should be able to see "Cluster0" we made in the previous steps. Now we need to create a database inside that cluster. We can do it by clicking "Cluster 0" as shown on the image below.

MongoDB Connect
MongoDB Connect

After that, we will navigate to "Collections" tab of the Cluster. Then since we do not have any databases create yet we can click "Add My Own Data" in order to create our todo database.

MongoDB Database Creation
MongoDB Database Creation

After you click that, the database creation modal should appear. You should enter the following data:

MongoDB Database Modal
MongoDB Database Modal

Here we defined our database name as todo_list and created a collection where we will store todo item called todos.


After we setup our database for todo list, we want to obtain the connection link so we can connect to MongoDB Cloud database from our Node.js application. To do that we will need to go back to cluster list and click "Connect" button on "Cluster0".

Cluster0 Connnect MongoDB
Cluster0 Connnect MongoDB

Next you should see the popup that asks you how you want to connect to your database. For this purpose we will use "Connect your application" option.

MongoDB Connect App
MongoDB Connect App

This should give you a new window with the MongoDB link and instructions on how to connect to it.

MongoDB Instructions
MongoDB Instructions

Awesome, we got our connection link! But, as you can see, we need to replace <password> with our password we created for test_user and myFirstDatabase with the name of the database we created todo_list.

So our final MongoDB connection URL will look like this:

mongodb+srv://test_user:Test12345@cluster0.axh68.mongodb.net/todo_list?retryWrites=true&w=majority

Now we need to test the connection from our Node.js application to this Mongo database link. To manipulate MongoDB, the idea is to use library called mongoose for Node.js which helps with object modeling of mongo databases. You can check it here https://mongoosejs.com/.

First, let's navigate to our server-app folder inside VS Code Terminal and add mongoose library to our application by executing

npm install mongoose --save

Then, we will update our index.js file to connect to the database via URL we created before.

const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const port = 3001;

app.use(cors({origin: '*'}));

const mongoDBLink = 'mongodb+srv://test_user:Test12345@cluster0.axh68.mongodb.net/todo_list?retryWrites=true&w=majority';

mongoose.connect(mongoDBLink);

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});
We now initialized and connected to Mongo database and we can now move on to the implementation of logic for adding todos, removing them, marking them as done and getting all todos from the database.
Last update: March 28, 2022