Home » Setup MongoDB in MEAN Stack

Setup MongoDB in MEAN Stack

by Online Tutorials Library

Setup MongoDB in MEAN Stack

In our previous section, we learned about MongoDB, the difference between SQL and NoSQL, and some of its features. In this section, we will connect angular to the database, and we will also learn how to setup MongoDB in MEAN Stack.

We cannot connect angular to the database directly without sending an http request to the node. We would have our angular app and our MongoDB database on a server. We would directly send queries to the database from our angular app and surpass our node express application. Technically, it would be possible but doing that is not good because it is highly insecure.

By doing this, the secure authentication is not possible. We have to log into the database or authenticate the database, and that is done through credentials which we would have to store in angular code. This angular code is then compiled to JavaScript in the end and loaded into the browser.

Everyone can view it in the browser, and every user of our web page can access our JavaScript code. Hence, we should not store important information or especially not credentials there. We would have no way of storing these securely. We would expose credentials, which then allow way more than we want it to allow. Therefore, our full database would be exposed, so users can probably then do more than we want them to do.

So, connecting angular to MongoDB is not a good idea. We will send an http request to the node where users cannot send a different one because the node only accepts the requests which we define it to accept, and then the node has all the credentials. But the nodejs code cannot be read by our users because it resides on a server.

We will use the following steps to set up the MongoDB in MEAN Stack:

1) We will go to the https://www.mongodb.com/try/download/community link and download it for our windows operating system.

Setup MongoDB in MEAN Stack
Setup MongoDB in MEAN Stack

2) We will use a different solution, i.e., cloud solution. This cloud solution doesn’t cost us anything and totally enough for our purpose. We need a paid solution for real apps, but that is also true if we host the server on our own.

This cloud solution name is MongoDB Atlas, and it is a cloud-hosted MongoDB database that we can conveniently use. We can then use it from our node express app, which we are building on our machine and will be deployed on the real server later.

Setup MongoDB in MEAN Stack

For using it, we need to sign in. After sign in successfully, we will see the browser like this:

Setup MongoDB in MEAN Stack

3) We will scroll down the page and click on the Create a cluster button to create a cluster. After clicking on the button, we will see the browser like this:

Setup MongoDB in MEAN Stack

If we don’t have a project, we need to create one.

Setup MongoDB in MEAN Stack
Setup MongoDB in MEAN Stack

We will click on the AWS and then click on the Create Cluster button at the bottom right corner. After clicking, cluster will be created, and we will see the browser as:

Setup MongoDB in MEAN Stack

4) We will click on the Database Access option of the SECURITY and create a user by clicking on the Add New Database User. When we click on the Add New Database User button, we will see the browser’s following screen:

Setup MongoDB in MEAN Stack
Setup MongoDB in MEAN Stack

5) We will scroll down the page and fill the username and password. We can auto-generate the secure password by clicking on the Autogenerate Secure Password. We will also need to set the database user privileges, and after doing all these things, we will click on the Add User button at the bottom-right corner of the page.

Setup MongoDB in MEAN Stack
Setup MongoDB in MEAN Stack

Note: We need to store the password somewhere because it will be required in our coding section.

6) We also need to add an IP address for it. We will go to the Network Access section of the page. Here, we will click on the Add IP Address button to add an IP. When we click on the button, we will see the following page on the browser:

Setup MongoDB in MEAN Stack
Setup MongoDB in MEAN Stack

7) Here, we will simply choose our current IP address. Keep in mind that if we work on that project over a couple of days, our IP address might change. So, we probably need to update it here. After adding the current IP address, we will simply click on the Confirm button for the confirmation of it.

Setup MongoDB in MEAN Stack
Setup MongoDB in MEAN Stack

Now, we will go back to our project. In our project, we already installed a package that helps us to connect to that backend database.

Note: If you face any connection issue, then you can update or change your IP address.

In the next section, we will learn about Mongoose and why we need to use it in our project.


You may also like