Connecting to MongoDB Atlas

Connecting to MongoDB Atlas

MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era.

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.

Click on the green button that says “Build a Cluster”. It will then ask you what type of cluster you want to create. Best MongoDB course from industrial experts.

-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.

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.

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. mongodb online training india for more effective learning.

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:


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.


You should be able to run your node server now and should be receiving your success message in the console. MongoDB online training helps you to learn more techniques and skills.