Skip to content

Connecting to the Cluster

Before we jack in, we need to know what we are connecting to.

In the MSSQL universe, the closest analog to a MongoDB Cluster is the SQL Server Instance.

  • SQL Server Instance: A single environment that hosts multiple databases.
  • MongoDB Cluster: A group of servers (called a Replica Set) that hosts multiple databases.

The key difference? A Cluster is a team. It typically consists of three servers holding the exact same data. If the leader (Primary) goes down, one of the backups (Secondaries) immediately takes command. We don’t connect to a single machine; we connect to the team.

To connect, we need a Connection String. This is a long URL that contains the address of the cluster as well as the credentials to access it.

  1. Go to the Atlas Cluster Dashboard.
  2. Click Connect.
  3. Click MongoDB Shell (mongosh).
  4. Copy the connection string provided. It looks like this:
Terminal window
mongodb+srv://<username>:<password>@cluster0.abc123.mongodb.net/
MongoDB Shell (mongosh) connection string

Fig 1: MongoDB Shell (mongosh) connection string.

Open your terminal and paste the command.

Replace <username> with the credentials saved earlier.

It will prompt you for the corresponding password .

Terminal window
mongosh "mongodb+srv://cluster0.abc123.mongodb.net/" --username <username>

When it asks for the password, type it in (it will be invisible) and hit Enter.

We will know we are in if we see a prompt that looks like this:

Terminal window
Enterprise cluster0-shard-0:PRIMARY>

We have breached the mainframe. (Okay, we just logged in).

Let’s get building. We have data to store.