DocsHub
Basics

MongoDB Atlas

Learn what MongoDB Atlas is, how to create a free cloud cluster, and how to connect to it from your app.

MongoDB Atlas

So far we have been running MongoDB locally on our own machine. That works fine for development, but when you build a real app that other people use, you need your database running somewhere on the internet — available 24/7, backed up, and secure.

MongoDB Atlas is the official cloud platform for MongoDB. It lets you run MongoDB on the cloud without managing any servers yourself. You just create a cluster, connect to it, and start using it.

Atlas has a free tier that is good enough for learning, side projects, and small apps.


Key Terms

Cluster — a group of servers that run your MongoDB database. Atlas manages these servers for you.

Free Tier (M0) — a free cluster with 512MB storage. No credit card needed. Perfect for learning.

Connection String — Atlas gives you a special connection string to connect your app to the cloud database. It works exactly like a local connection string but points to Atlas servers instead of localhost.


Create a Free Atlas Account

  1. Go to cloud.mongodb.com
  2. Click Try Free and sign up with your email or Google account
  3. Fill in your details and verify your email

Create a Free Cluster

Once you are logged in:

  1. Click Create a deployment
  2. Select M0 Free — the free tier
  3. Choose a cloud provider (AWS, Google Cloud, or Azure — any works)
  4. Choose a region close to you for better speed
  5. Give your cluster a name — for example school-cluster
  6. Click Create Deployment

Atlas will take about 1–3 minutes to set up your cluster.


Create a Database User

Atlas requires a username and password to connect — it will not let you connect without one.

  1. In the setup screen, you will see Create a database user
  2. Enter a username — for example schooladmin
  3. Enter a strong password — copy it somewhere safe
  4. Click Create Database User

Save your username and password somewhere safe right now. You will need them in your connection string. If you lose the password, you will have to reset it from the Atlas dashboard.


Allow Your IP Address

Atlas blocks all connections by default. You need to tell it which IP addresses are allowed to connect.

  1. In the setup screen, click Add My Current IP Address
  2. This adds your current IP so your machine can connect
  3. Click Finish and Close

If you are building an app that will be deployed on a server, you will need to add that server's IP address here too. For development, just add your own IP. You can always add more IPs later from Network Access in the Atlas dashboard.

For development only, you can allow connections from anywhere by adding 0.0.0.0/0 as the IP address. This is convenient but not recommended for production apps.


Get Your Connection String

  1. Go to your cluster and click Connect
  2. Choose Drivers
  3. Select Node.js as the driver
  4. Copy the connection string — it looks like this:
mongodb+srv://schooladmin:<password>@school-cluster.abc12.mongodb.net/?retryWrites=true&w=majority

Replace <password> with the actual password you created.


Connect from Node.js

Install the MongoDB driver if you have not already:

npm install mongodb

Create a .env file in your project:

MONGODB_URI=mongodb+srv://schooladmin:yourpassword@school-cluster.abc12.mongodb.net/school?retryWrites=true&w=majority

Install dotenv to read the .env file:

npm install dotenv

Now connect to Atlas in your Node.js app:

require('dotenv').config();
const { MongoClient } = require('mongodb');

const client = new MongoClient(process.env.MONGODB_URI);

async function main() {
  await client.connect();
  console.log('Connected to MongoDB Atlas');

  const db = client.db('school');
  const students = db.collection('students');

  await students.insertOne({
    name: "Ali Hassan",
    age: 16,
    grade: "10th",
    subjects: ["Math", "Physics"]
  });

  console.log('Student inserted');
}

main()
  .catch(console.error)
  .finally(() => client.close());

Run it:

node connect.js

Output:

Connected to MongoDB Atlas
Student inserted

Your data is now stored in the cloud.


Connect from mongosh

You can also connect to your Atlas cluster directly from mongosh:

mongosh "mongodb+srv://schooladmin:yourpassword@school-cluster.abc12.mongodb.net/school"

Once connected, everything works exactly the same as local — same commands, same queries, same everything. The only difference is where the data lives.


Atlas vs Local — What to Use When

SituationUse
Learning and experimentingLocal MongoDB
Building and testing your appLocal MongoDB
Sharing your app with othersAtlas
Production appsAtlas
Working from multiple machinesAtlas
Need backups and monitoringAtlas

For this guide, local MongoDB is fine for all the examples. Use Atlas when you want to deploy something real.


The Atlas Dashboard

Once your cluster is running, the Atlas dashboard gives you useful tools:

Collections — browse your databases and documents visually, just like Compass but in the browser.

Metrics — see how many operations per second, memory usage, and connection counts your cluster is handling.

Backups — Atlas automatically backs up your data. You can restore to any point in time.

Network Access — manage which IP addresses can connect to your cluster.

Database Access — manage database users and their permissions.


Add your .env file to .gitignore immediately. Your connection string contains your password — never commit it to GitHub. Anyone who gets that string has full access to your database.

On this page