Connect your NodeJS application with MongoDB Atlas Cluster
Tags: node, mongodb, javascript
Published: 2020-06-04 11:26:43

##**Create a MongoDB Atlas account**
Once you open the [MongoDB Atlas](https://www.mongodb.com/cloud/atlas) page, you should [sign in](https://account.mongodb.com/account/login) / [register](https://account.mongodb.com/account/register) for a new account

After logged into your new account click **`Create a cluster`** under `Shared Clusters`

![Free Shared Cluster](https://dev-to-uploads.s3.amazonaws.com/i/u1k7sbyj4843uxb86cz7.png)

##**Create a new cluster**
• **Choose your cloud provider and region**, you can leave this as the default provided (typically AWS).

![Cloud Provider & Region](https://dev-to-uploads.s3.amazonaws.com/i/k98lhr5q6lw7pcp2crjl.png)

• **Customize your cluster's specs**, you can also leave this as the default provided, `M0 Sandbox (Shared RAM, 512 MB Storage) Encrypted`.

![Cluster Tier](https://dev-to-uploads.s3.amazonaws.com/i/ouo8tl1vlndwiom8jauj.png)

• **Give your cluster a name**, you can also leave this as the default provided, `Cluster 0`

![Cluster Name](https://dev-to-uploads.s3.amazonaws.com/i/lo8qm10d8ry6920p79av.png)

• Now click the green **Create Cluster** button at the bottom of the screen and verify the image captions they provide.

![Create Cluster](https://dev-to-uploads.s3.amazonaws.com/i/odgyluy5cu420lapwke4.png)

• You should now see the message, `Your cluster is being created - New clusters take between 1-3 minutes to provision`. Wait until the cluster is created before going to the next step

![Cluster is being created](https://dev-to-uploads.s3.amazonaws.com/i/g5hj4awqnicv0wx7vaa2.png)

##**Create a new user on the database**
• Click on `Database Access` Under **`SECURITY`** tab on the left side of the screen

![Database Access](https://dev-to-uploads.s3.amazonaws.com/i/zpeqnk4bjumtoom9sg6x.png)

• Under `Database User` tab click on big green **`Add New Database User`** button

![Add New Database User](https://dev-to-uploads.s3.amazonaws.com/i/ap2p0a11gmc61h8mmbde.png)

• Enter a `user name` and `password` and then select **`Read or write to any database`** under user privileges, remember to **store** your username and password somewhere safe.

![Authentication Method](https://dev-to-uploads.s3.amazonaws.com/i/xvs5lspwl1nesf3551cu.png)

• Click on the **`Add User`** green button in the bottom right of the modal.

Note: You can always upgrade your privileges to the **Admin** level, however, it is best practice to give permissions to a user on an as-needed basis for security reasons.

##**Whitelist your IP address**
On the left side, Under `SECURITY` click on **`Network Access`**

![Network Access](https://dev-to-uploads.s3.amazonaws.com/i/up89359so87o9z17hurq.png)

Click on green **`Add IP Address`** button under under `IP Whitelist` tab

![Add IP Address](https://dev-to-uploads.s3.amazonaws.com/i/2xbxqjk8bkccff6st5xc.png)

In the modal, click the **`ALLOW ACCESS FROM ANYWHERE`** button and you should see `0.0.0.0/0` pre-filled for the whitelist entry field, click the green `Confirm` button

![IP Whitelist Entry](https://dev-to-uploads.s3.amazonaws.com/i/iql0vtwi3b7wpabbr2vd.png)

##**Connect to your cluster
Click on **`Clusters`** under `DATA STORAGE`

![Clusters](https://dev-to-uploads.s3.amazonaws.com/i/4hmokosm5vmqavv2dzo9.png)

Follow the instructions by clicking on the **`CONNECT`** button in the Sandbox section

![Sandbox](https://dev-to-uploads.s3.amazonaws.com/i/xoofwbijaygkc1urw0gi.png)

In the pop-up modal, click on **`Connect Your Application`**, a connection string will be displayed, you can copy that connection string by clicking on the `copy` button.

![Connect Your Application](https://dev-to-uploads.s3.amazonaws.com/i/6ivictbodvxdgc6fsw5n.png)

This will be the final URI that you will use to connect to your db, it will look something like this `mongodb+srv://<user>:<password>@<cluster#-dbname>...`, notice that the user and cluster#-dbname fields are already filled out for you, all you would need to replace is the `password` field with the one that you created in the previous step

![Connetion String](https://dev-to-uploads.s3.amazonaws.com/i/ly3bfdg9y3nkaer177uz.png)

That's it! You now have the URI you will add to your application to connect to your database. Keep this URI safe somewhere, so you can use it later!