So far, we have not been able to persist any data or make our app properly dynamic. However, that will change as we integrate the database layer for our server using MongoDB.
But why MongoDB? You may have heard that it is a NoSQL database, but what does that mean? What is a SQL database in the first place? For that matter, what is a database?!
What Is a Database?
A database is a collection of structured data. The structure and how it's organized depends on the type of database. Two types of databases are of interest here: SQL and NoSQL.
What Is an SQL Database?
An SQL database (generally pronounced "sequel") is a relational database that uses Structured Query Language to manage its data, typically organized into tables according to strict schemas. For example, if you decide that users have a first name, last name, and email address, you will have a Users table with first_name
, last_name
, and email
columns, with each row corresponding to a single user. To be considered valid, each user must provide that information so you can be certain that every user you draw from the database will have those attributes.
In an SQL database, relationships between different tables are critical. Therefore, your Users table will also have an id
column for each user's unique individual identifier. This means that you can reference a user from another table. For example, if you have an Orders table, each order must belong to a user. Therefore, you will reference the user's identifier in the user_id
column of the Orders table.
SQL databases (like MySQL or PostgreSQL) are great for relational data and where strong definitions are needed. However, in the context of smaller projects, MVPs, or startups, you may not know what your final data model will look like. You also might want to scale quickly (it is challenging to scale an SQL database beyond a certain limit) to handle more users. That is where NoSQL comes in, particularly MongoDB.
What Is MongoDB?
MongoDB is a NoSQL database. That means that you cannot use SQL to communicate with it. Rather, it is a document-based system, where data is stored as collections of individual documents described in JSON (JavaScript Object Notation). There are no strict data schemas (you can write anything you want anywhere you want), and there are no built-in relationships between pieces of data. However, there are tools to help you overcome these obstacles, which you will discover soon enough!
The major advantages of using MongoDB are its scalability and flexibility. The official website describes MongoDB as being "built for people who are building internet and business applications who need to evolve quickly and scale elegantly." Therefore, it's a sought-after skill in startups and small businesses. Another advantage is how easy it is to communicate with the database using JavaScript with documents described in JSON. This allows you to apply the JS knowledge you already have to the database layer!
Right, that's enough theory. Let's get cracking!
Set Up MongoDB Atlas
While it is possible to download and run MongoDB on your own machine (see the MongoDB website for details), we will be using the free version of MongoDB Atlas — the database-as-a-service — for this course.
Head over to the MongoDB Atlas website and sign up for a free account. Once you have access to your dashboard, create a new cluster, and set it up with the AWS option and only free tier options to keep development free.
While your cluster is spinning up, you can go to the Security tab. First, you'll want to add a new user with the ability to read and write to any database. You can choose any username and password, but write it down, as you will need it to connect your API to your cluster.
You will also need to go to the IP Whitelist tab and add your current IP address (which you will need to update whenever it changes) or allow access from anywhere.
Once your cluster has finished being created, you can move on to the next section.
Connect Your API to Your MongoDB Cluster
Are you ready to discover in the next video, how to connect your API to your MongoDB Cluster? Let's go!
From your MongoDB Atlas, click the Connect button, and choose "Connect your application." You can then select "driver 3.6 or later" and copy the generated SRV address.
Back in your project, install the Mongoose package by running:
npm install mongoose
from within the backend
folder.
Once it has finished installing, import it into your app.js
file by adding the following constant:
const mongoose = require('mongoose');
Just beneath your app
constant declaration, add the following line, replacing the SRV address with your own, and the <PASSWORD>
string with your MongoDB user password:
mongoose.connect('mongodb+srv://will:<PASSWORD>@cluster0-pme76.mongodb.net/test?retryWrites=true')
.then(() => {
console.log('Successfully connected to MongoDB Atlas!');
})
.catch((error) => {
console.log('Unable to connect to MongoDB Atlas!');
console.error(error);
});
After saving this and restarting your server if necessary, you should see "Successfully connected to MongoDB Atlas" logged to the console. Your API is now connected to your database cluster, and you can start creating some server routes to take advantage of this.
Let's Recap!
NoSQL databases like MongoDB are often a good technical choice for fast-evolving apps.
MongoDB Atlas is a great free solution for free MongoDB database hosting.
The Mongoose package facilitates interactions between your Express app and your MongoDB database.
Now that you have configured your database, let's see how to create a data schema to make your application robust in the next chapter!