Connecting to MongoDB Atlas

Sergio Perez
4 min readFeb 16, 2020

--

Quick post on how to connect to MongoDB Atlas from a NodeJS/Express application.

What is MongoDB Atlas?

It is a global cloud database service developed by MongoDB. You are given the option to choose the cloud service provider. Atlas will handle the complexity of deploying, managing, and healing your deployments.

Getting Started

First of all you will have to make an account on their site.

Next up is creating a cluster. Click on the green button that says “Build a Cluster”. It will then ask you what type of cluster you want to create.

-Starter Cluster

-Single-Region Cluster

-Multi-Region Cluster

We will go with a Starter Cluster since it is free and we will only be using it for a personal project.

Then it will ask what cloud provider you would like to use. For my case I will be using Google’s Cloud Platform, you have the option to choose AWS or Azure. I will be using the “Iowa” region since it has the “free tier available” tag. You do not need to touch the Cluster Tier or the Additional Settings. Feel free to change the Cluster Name if needed.

It may take 1–3 minutes for the cluster to be created. Once it is done you should be greeted with the following screen.

Click on the “Connect” button right under the cluster name. It will give you 3 options to connect to it from your application. I will be going with the 2nd one “Connect Your Application”, this option will give you a connecting string and examples on how to use it.

Driver option should be set to Node.js and the version should be 3.0 or later. You will need to copy the connection string.

Example of what my connection string looks like:

“mongodb+srv://SergioP:<password>@cluster0–41rmx.gcp.mongodb.net/test?retryWrites=true&w=majority”

You will need to change the <password> text with your password that you set up when creating the MongoDB Atlas user.

Connecting it to your application

I have a simple NodeJS application with one route for example purposes. Preferable you will want to connect to the MongoDB Atlas cluster before Node listens for your server.

Also keep in mind that we will be using mongoose to connect. So if you need to install it you can do so with the following command lines.

npm install mongoose

And require it in your server js file.

const mongoose = require('mongoose');

Now with the connection string we copied we will insert the following code:

const uri = “YOUR CONNECTION STRING“;
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true}).then(() => { console.log(‘MongoDB Connected…’)}).catch(err => console.log(err))

Using the connect method that mongoose provides us with we are first passing in the connection string. Then we are passing some parameters useNewUrlParser and useUnifiedTopology.

The connect method will return a promise, we will need to handle that promise. Attaching a “.then” after the method we can console.log a success message if all goes well. We have also attached a “.catch” after, that will catch any errors if we cannot connect to our MongoDB Atlas Cluster.

Last Step Network Access

This last step is still a bit tricky for me and I assume it acts as an extra step of security. Back on the MongoDB Atlas dashboard click on “Network Access” under “Security”.

You want to click on “Add Current IP Address” and it will automatically generate your IP address into the field, then click “confirm”. You also do have the option to allow access from anywhere. I used this a couple times when I was testing out my connection. MongoDB will email you and tell you how dangerous this is. So I do not recommend it unless you are quickly testing it.

Conclusion

You should be able to run your node server now and should be receiving your success message in the console.

I have not deployed my project where I am using this MongoDB Atlas Connection so I am uncertain if it will break once it gets deployed.

Also, I have had to log back into the MongoDB dashboard and update my IP address when I work in a different location.

--

--

Sergio Perez

I like creating things and love cool visuals/designs.