Setting Up a MongoDB Database
Published: 2020-09-16 20:24:50

Are you new to MongoDB? Perhaps, like me, you came from a SQL based database? I am writing this article to help get you started with MongoDB as it is a little bit different than SQL and the set up can be confusing at first. Let’s dive in!


#Stack#

The stack I will be using for this article is MERN although React won’t be necessary. Lets set up a new project first. First make a new directory to house your project. I called mine `mongodb-setup` so:
`mkdir mongodb-setup` 


#Setting Up Your Server#

Once your directory `cd` into it and 
`npm init`
to integrate Node and any npm libraries. Now lets install the mongoose library 
`npm install mongoose`
Mongoose is the library we use to control, manipulate, and pass data to and from our MongoDB database. You should see a node_modules directory create in your project. 


Next, we need to create a file that controls all server related tasks. 
`touch server.js`
Since we are going to use Experess to host our server lets install that as well. 
`npm install express`
Now lets set up our server. 


At the top of `server.js` add 
`const express = require(’express’)`
underneath that add 
`const app = express()` 
Finally, the last thing we need to do is connect our server. Add 
`app.listen(8080, () => { console.log(‘A Node JS API is listening on port 8080’)})` 
Now run `npm start` if your server was set up properly you should see the message ` A Node JS API is listening on port 8080` appear in your console which means express was set up correctly. Congrats!


#Using MongoDB Atlas #

In this section we are going to utilize MongoDB atlas to create a cluster that will house all of our data. Head over to https://www.mongodb.com/cloud/atlas. Make an account

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xq7wlt5xgh14bd1lzgcj.png)

Once you sign in you will be brought to the cluster selection page. If you are just testing out the database then you probably want to select the free version.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0k06ykzsocr2r5337kr2.png)

From this page you can choose a provider. In this case I am picking AWS, then select a recommended location below. Then click CREATE CLUSTER`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vkpxzue8z4vsul7nskng.png)

It will take a few minutes to create the cluster. In the meantime, lets setup a .env file in our app. In your app type 
`npm install dotenv` 
in the terminal. Next, 
`touch .env`
to create the file.  Inside of .env lets get the DBURI set up. Add 
`MONGO_URI=` 
we will be adding something here shortly, but for now let’s leave it at that. 


Now, let’s configure the cluster so that we have access to the database. Head over to Database Access under the Security section on the left. 


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/88nbfml50nvf0x0fokrb.png)

We need to add our self as a user in order to grant access to the database. Now, create a username and super-secret password and click Add User.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/med72ogakew0rbuul56b.png)

Next, we need to whitelist your database so that it will accept and grant access to requests. Head to Network Access directly under Database Access

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/111008y1kmj0l9cz7ya6.png)

Click ADD IP ADDRESS then, ALLOW ACCESS FROM ANYWHERE. Finally, click Confirm. 
	
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ydh340rg1bu43ph77262.png)

Now, let’s connect our database to our app. Head back to the Cluster tab and select the connect button. This will bring up a modal on which you will select Connect Your Application.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xhcnrwr2zwtdp2zux5sn.png)

Make sure you copy the connection string provided by clicking the clipboard.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1z04o5ky7hh2qxmlccgo.png)

#Linking App to Database#

Now, we can connect the database to our app. Head back over to your app and open the .env file. Lets add the connection string to MONGO_URI. So in my case this is what I’ve got: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sbyibl2gb2zdgat1bmdg.png)

We need to change <password> to the password we created when we added ourselves as a user. Notice how my code changes:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ztjodj0z741pvt63fdow.png)

Next, we need to add a few more packages. In server.js add the following:

`const mongoose = require(‘mongoose’)`
`const dotenv = require(‘dotenv’) 
`dotenv.config`


Remember, mongoose is a library for MongoDB. The dotenv configuration is so that we can use our MONGO_URI constant from the .env file. 


Lastly, We need to add two more blocks of code to finish this connection. Below that exemplifies those final blocks. You can just copy it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2it2tcqvyg2ukd9pw4zm.png)

Our finished product should look like the following:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/orspodzej5o23hk88nhe.png)

Once that is completed run `npm start` you should see the messages “A node JS API is listening” and “DB Connected” in the terminal. If you’ve got that then congratulations you are done.


You are now able to make requests to your database! If you find yourself getting stuck you can visit my repo here: https://github.com/Aaidenplays/mongodb-setup

I hope you found this article helpful! Thank you for reading.