After this lesson, you will be able to: Set up a free MongoDB Atlas cluster, configure access, and connect from an app.
Atlas is Mongo's managed cloud DB. Free M0 tier is fine for portfolio + dev. Setup takes 10 minutes and replaces 'how do I host Mongo?' forever.
Step by step.
1. Sign up at mongodb.com/atlas (free).
2. Create organization + project.
3. Build a Database → M0 free tier → pick region near you.
4. Cluster spins up in ~3 minutes.
5. Database Access → add user + password (save these securely).
6. Network Access → allow your IP, OR 0.0.0.0/0 for dev (NEVER prod).
7. Connect → choose driver → copy connection string.
8. Replace <password> in the string with your DB user password.
Mongoose / driver.
// .env// MONGO_URI=mongodb+srv://user:pass@cluster.mongodb.net/myappimport mongoose from 'mongoose';await mongoose.connect(process.env.MONGO_URI!);console.log('Connected');// Raw driverimport { MongoClient } from 'mongodb';const client = new MongoClient(process.env.MONGO_URI!);await client.connect();const db = client.db('myapp');
Mongoose maintains a connection pool, reuse the same connection across requests. On Vercel/Lambda: cache the connection across invocations. Don't connect on every request. Pattern: a module-level `clientPromise` that resolves once + is reused.
Whitelist only your app's IPs (or use Atlas's VPC Peering / Private Endpoint). Rotate DB user passwords; don't commit them. Enable backups (free tier has continuous backup since 2024). Monitor with Atlas's built-in performance advisor, it suggests missing indexes. Upgrade past M0 if you need IP whitelisting per stage, more storage, or sharding.
Using 0.0.0.0/0 IP whitelist in production (open to the internet). New connection per request on serverless (exhausts connection pool). Hardcoding the connection string in source. Forgetting to URL-encode special chars in the password.
Sign in and purchase access to unlock this lesson.