DEV Community

Cover image for Connect your NodeJS application with MongoDB Atlas Cluster
Shawon Saha
Shawon Saha

Posted on

Connect your NodeJS application with MongoDB Atlas Cluster

Create a MongoDB Atlas account

Once you open the MongoDB Atlas page, you should sign in / register for a new account

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

Free Shared Cluster

Create a new cluster

Choose your cloud provider and region, you can leave this as the default provided (typically AWS).

Cloud Provider & Region

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

Cluster Tier

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

Cluster Name

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

Create Cluster

• 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

Create a new user on the database

• Click on Database Access Under SECURITY tab on the left side of the screen

Database Access

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

Add New Database User

• 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

• 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

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

Add IP Address

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

**Connect to your cluster

Click on Clusters under DATA STORAGE

Clusters

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

Sandbox

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

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

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!

Top comments (0)