Connect MongoDB Atlas with MongoDB Compass

If your app is using MongoDB, MongoDB Atlas is the best method to host it. We can easily deploy a database and manage it in MongoDB Atlas. It provides a versatile tool MongoDB Compass to interact with the DB using GUI. Here we will discuss the steps to connect MongoDB Atlas with MongoDB Compass.

Prerequisites

Before continuing this article, I assume that the reader has basic knowledge in creating a MongoDB Atlas account and setting up a database or collection in it.

Otherwise please refer to my previous article, Connect MongoDB Atlas With Express Backend. Skip the Express.js and section.

What we will learn

After completing this article, we will learn the steps to:-

Create MongoDB Atlas cloud database

At first, we are going to create a cloud database on MongoDB Atlas. It providers the easiest way to deploy, operate and scale MongoDB in the Cloud.

Let us discuss it step by step with screenshots and explanations.

Create an account on MongoDB Atlas

We need a MongoDB account for creating a database. So, log on to the MongoDB page and Sign Up for an account. Or we can simply create an account and log in using our Google account.

Setup our MongoDB account

After creating an account, we will see a page to enter our organization and project details. We will also get an option to choose our preferred programming language. Because we are using Node/ Express.js backend, we can choose JavaScript in this category.

Create a cluster

The word cluster may confuse you. It is actually a server where MongoDB stores our data. Choosing the right plan before creating a cluster is important. Let us discuss about the plans first.

  • Dedicated Multi-Cloud & Multi-Region Cluster – If we are developing a project for global users, then choosing this plan is great. Because it stores the data in dedicated servers located in multiple regions. This will increase the speed of our app.
  • Dedicated Cluster – A dedicated cluster is a single server only dedicated to our organization/project. It can provide advanced performance.
  • Shared Cluster – Shared Clusters are dedicated to a number of users. So it will affect the performance of our app in peak timings.

Because we are in a development mode, Shared Cluster is ok for us. It is Free.

It will direct to another page and from here, we can choose the Provider and Region, RAM etc.

We can choose from AWSGoogle Cloud, and Azure.

Because we are not choosing a paid plan, create a cluster with default settings which is free.

Create a collection

So the cluster is created and now we need to create a collection. We discussed that a cluster is a server for storing our project databases.

A collection is simply the collection of databases associated with each project we are going to create. A cluster can contain multiple collections.

So from the cluster, we created, click on the COLLECTIONS tab.

We will get an option to load a Sample Dataset and our own data. Choose to Add My Own Data from the options.

This will open up a modal for creating a database. I am giving posts as database name and posts and collection names.

Connect to the cluster

Now we can look for the steps to connect this database with our backend application. For this, we need a connection string.

Before getting a connection string, we need to complete some steps.

At the top-right, we can see a tab Cmd Line Tools. Click on it, and it will show us a page as below.

Click the Connect Instructions button.

It will display a view as below, where we should choose the option Connection your application.

For setting up the connection securely, we can set an IP address of the server where our backend is running. So that the request to the database can only be done from the server.

But in our case, we are developing our backend locally. So, let us choose, Add your current IP Address. So that we can use the database locally.

Note that, if a number of developers are accessing the DB from different IP addresses, Choosing the option Allow access from Anywhere is preferred.

Now we can create a database username and password. This data will be used in our connection string to maintain secure access.

At the next screen, we will get a connection string that can be used to connect with MongoDB Compass.

The copied connection string will be in a format as below.

mongodb+srv://<username>:<password>@cluster0.7cpxz.mongodb.net/test

Download and Install MongoDB Compass

Now let us download and install MongoDB Compass on our system. I am providing the URL to the official website to download the MongoDB Compass.

https://www.mongodb.com/try/download/compass

After installing it on our system, we can see a window shown below. Here we can paste the connection string that we copied from the previous step.

Setup a new connection with the connection string

Now let us create a new connection with the connection string that we have already copied from the previous step.

Paste the connection string in the input field in MongoDB Compass.

Before pasting the connection string, we need to replace the username and password with the one we have created before.

In our case, the username is techomoro and password is a random string. So the connection string will be the same as below.

  • username: techomoro
  • password: uWfMzw2oiIahWlS5
mongodb+srv://techomoro:[email protected]/test

After creating a connection, the window will be the same as below. It will connect to the database test and we can see the users collection inside it. It is a dummy collection I have made for test purpose.

Summary

In this article, we explained the method of creating a MongoDB Atlas account and a collection in it. Then we downloaded and set up MongoDB Compass and learned to connect the MongoDB Atlas with MongoDB Compass.

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.