Day 11: NodeJS - All about MongoDB Atlas

MongoDB Atlas is a service provided by MongoDB which gives us a cloud-based database. Our database will be in the cloud which effectively means the MongoDB Server.

You may be wondering why there MongoDB Atlas is referred to here. The thing is, whatever data we are going to generate or use while performing NodeJS operations, we are going to store that data in this cloud-based database.

Let us start using MongoDB Atlas. Search for MongoDB Atlas and sign up yourself with Google ID. Once you log in after registering yourself, you will see the following screen.

To view your "Clusters", click on "All Clusters" on the navigation bar on top.

A Cluster is basically a collection of databases, which we try to run on a particular instance. Let us try to build a database here. Click on the "Build a Database" button.

After clicking on that button, we will see the following screen:

We will be needing to select the FREE option, as we are using the database for our learning purpose. After selecting it, click the Create button below.

Once you click the create button, it will list out some security provisions you will need to provide. It will ask for Username, Password for a user to be added to use the database, from where you want to connect, the Local environment or Cloud Environment, and your IP address.

Input any username and password you like for the user. Later select the Local environment as an option to connect from. Later while adding the IP address, click on the "Add my IP Address" button. This will add an entry of your machine's IP address that you can use.

As you can see, our machine's local IP Address has been added. But we will do a small change. We will add another entry as shown below.

This means universal. The purpose of the first IP Address is to give access to the database to the machine if the IP Address matches. Just because you have a username or password does not finish things here. By adding the second entry as shown above, you are telling me that this database can be accessed on ANY IP ADDRESS after I provide my username and password. The reason we do this is that the first entry is a dynamic IP address. It will change if I restart the machine. And thus we will start getting errors about being unable to connect to the database. In order to avoid this issue in the near future, we are doing this part.

Click on "Finish and Close" once this is all done. You will see the following screen.

Click on the "Connect" button as in the above image. Once you click it, you will see the following popup.

You may select the option you think suits you. I am selecting the "Compass". MongoDB Compass is a GUI application. By using this application, we will connect our cloud database to the app which we wrote. We will be easily able to create and work with databases with MongoDB Compass. Once you select the Compass option, the following popup appears:

If you have MongoDB Compass, then select the option accordingly. If not, it will tell you to choose your operating system and after clicking the download button, you will be able to install it.

Secondly, the step-2 URL is the link that we will be using in the MongoDB Compass, to establish the connection with our database. Same URL we will be using in our JavaScript code too. We have written all the code, but we have to store that data in the database too.

Once it is installed, you will be able to run the MongoDB Compass inside your machine. It will look something like this:

Notice the "URI" (Uniform Resource Identifier) localhost:27017. The reason we can need this is that if we were to use MongoDB on our OWN machine. At this step, we have only installed MongoDB Compass, which is a GUI tool that helps us to establish the connection to MongoDB Database.

Now we will copy the connection string from our previous image and only replace the <password> with the password that we set. After that click on the "Connect" button. Once the connection has been established, we will see the following window.

Lets us create a Database here. To do so, click on "+" in the Left Menu in front of the Database. It will give us this window:

Simply hit the "Create Database" button. You will be able to see this view.

Here on the left-hand side, you can see the database name and students as the Collection. A Collection is similar to a Table. Similarly, the term Documents is similar to Records in a Table.

Once you are ready to add the data, click on the Add data button at the top.

We will be inserting it in the form of JSON. So I am clicking on "Insert Document". Let us insert data now. You can refer the sample change I did below:

Click on Insert. You will see the following change on the main page.

To check if the collection is there, Go to MongoDB Atlas on the Web browser. Click on Cluster0 and then click on Collections navigation. You will see the data as below:

Notice that it has created its own ID as MongoDB works highly with ID and Objects. The purpose of doing so is to make it unique across all the objects.

Thus we can see our created database via MongoDB Compass can be seen on the Web via MongoDB Atlas. We will see the database operations later on in next articles.