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
- Go to cloud.mongodb.com
- Click Try Free and sign up with your email or Google account
- Fill in your details and verify your email
Create a Free Cluster
Once you are logged in:
- Click Create a deployment
- Select M0 Free — the free tier
- Choose a cloud provider (AWS, Google Cloud, or Azure — any works)
- Choose a region close to you for better speed
- Give your cluster a name — for example
school-cluster - 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.
- In the setup screen, you will see Create a database user
- Enter a username — for example
schooladmin - Enter a strong password — copy it somewhere safe
- 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.
- In the setup screen, click Add My Current IP Address
- This adds your current IP so your machine can connect
- 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
- Go to your cluster and click Connect
- Choose Drivers
- Select Node.js as the driver
- Copy the connection string — it looks like this:
mongodb+srv://schooladmin:<password>@school-cluster.abc12.mongodb.net/?retryWrites=true&w=majorityReplace <password> with the actual password you created.
Connect from Node.js
Install the MongoDB driver if you have not already:
npm install mongodbCreate a .env file in your project:
MONGODB_URI=mongodb+srv://schooladmin:yourpassword@school-cluster.abc12.mongodb.net/school?retryWrites=true&w=majorityInstall dotenv to read the .env file:
npm install dotenvNow 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.jsOutput:
Connected to MongoDB Atlas
Student insertedYour 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
| Situation | Use |
|---|---|
| Learning and experimenting | Local MongoDB |
| Building and testing your app | Local MongoDB |
| Sharing your app with others | Atlas |
| Production apps | Atlas |
| Working from multiple machines | Atlas |
| Need backups and monitoring | Atlas |
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.