MongoDB Atlas (Cloud) Get Started
MongoDB Atlas is MongoDB’s official fully-managed cloud database service: instead of installing and administering mongod yourself, Atlas provisions, patches, backs up, and monitors a replica set for you on AWS, Azure, or Google Cloud. It matters because almost every real MongoDB deployment today runs on Atlas rather than on self-managed servers, and its free tier (M0) lets you practice everything in this course — and build real projects — without installing anything locally or paying a cent. This lesson walks through creating an account, spinning up a cluster, securing access to it, and connecting from both mongosh and a Node.js application.
Overview / How it works
Atlas is not a different database engine — it runs the exact same MongoDB server (7.0+ as of this writing) that you would run locally, so every command you learn in this course (find, insertOne, aggregation pipelines, indexes) works identically. What Atlas adds is the operational layer: it automatically provisions a replica set (a primary plus two or more secondaries, even on the free tier) so your data survives a node failure, it terminates TLS so every connection is encrypted in transit, it encrypts data at rest, and it exposes a web console (the Atlas UI) for browsing data, building indexes, viewing performance metrics, and managing users.
To reach a cluster you don’t connect to a single machine; you connect through a connection string using the mongodb+srv:// scheme. The +srv part tells the driver or mongosh to look up a DNS SRV record that resolves to the current list of replica set members — this is why Atlas connection strings don’t list individual hostnames and keep working even as Atlas swaps nodes during maintenance or failover.
Atlas layers two independent access controls in front of every cluster, and both must be satisfied before a connection succeeds:
- Network Access (IP Access List) — a firewall rule. Even with perfect credentials, a connection from an unlisted IP address is dropped before authentication is ever attempted.
- Database Access (database users) — separate from your Atlas login (the account you use to sign into the web console), a database user is a username/password (or certificate) pair with roles scoped to specific databases, checked by the MongoDB server itself.
Understanding that these are two separate gates is the single most useful mental model for debugging Atlas connection problems: a timeout points at Network Access, while an authentication error points at Database Access.
Syntax
The general shape of an Atlas connection string is:
mongodb+srv://<username>:<password>@<cluster-address>/<database>?<options>
| Part | Meaning |
|---|---|
mongodb+srv:// |
Scheme indicating DNS-seedlist discovery of the replica set members; always used for Atlas. |
<username>:<password> |
Credentials for a database user created in Atlas’s Database Access tab, not your Atlas login. |
<cluster-address> |
The cluster’s unique hostname, e.g. cluster0.ab1cd.mongodb.net, copied from the Atlas “Connect” dialog. |
/<database> |
The default database the connection operates against; omitting it silently defaults to a database named test. |
?<options> |
Query-string options such as retryWrites=true&w=majority, which Atlas includes by default for safer writes. |
Examples
1. Connect to your cluster with mongosh
After creating a free M0 cluster, adding your IP to Network Access, and creating a database user, open a terminal and paste the connection string from Atlas’s “Connect > Shell” dialog:
mongosh "mongodb+srv://<user>:<password>@<cluster-url>/mydb"
Output:
Current Mongosh Log ID: 66f1a2b3c4d5e6f7a8b9c0d1
Connecting to: mongodb+srv://<cluster-url>/mydb?appName=mongosh+2.3.0
Using MongoDB: 7.0.12 (API Version 1)
Using Mongosh: 2.3.0
For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/
Atlas atlas-ab12cd-shard-0 [primary] mydb>
The prompt confirms two things at once: mydb shows which default database you’re in, and [primary] confirms mongosh routed your connection to the current primary node of the replica set, resolved automatically via the SRV lookup.
2. Load and explore the sample dataset
Atlas can load free sample datasets into any cluster (via the UI’s “Load Sample Dataset” button). Once loaded, explore it from the shell:
show dbs
use sample_mflix
show collections
Output:
admin 40.00 KiB
config 108.00 KiB
local 40.00 KiB
sample_mflix 500.16 MiB
switched to db sample_mflix
comments movies
sessions theaters
users
show dbs, use, and show collections are mongosh-only shortcuts — convenient to type interactively, but not real JavaScript, so you can’t put them inside a script. Now run an actual query, which is plain JavaScript:
db.movies.findOne({ title: "The Matrix" });
Output:
{
_id: ObjectId("573a1394f29313caabcd68d0"),
title: 'The Matrix',
year: 1999,
genres: [ 'Action', 'Sci-Fi' ],
cast: [ 'Keanu Reeves', 'Carrie-Anne Moss', 'Laurence Fishburne' ],
runtime: 136,
released: ISODate("1999-03-31T00:00:00.000Z")
}
3. Connect from a Node.js application
Install the driver with npm install mongodb, then use the same connection string from your application code:
import { MongoClient } from "mongodb";
const uri = "mongodb+srv://<user>:<password>@<cluster-url>/mydb?retryWrites=true&w=majority";
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
const db = client.db("mydb");
const result = await db.collection("users").insertOne({
name: "Ava",
plan: "free",
createdAt: new Date(),
});
console.log(result.insertedId);
} finally {
await client.close();
}
}
run();
Output:
new ObjectId("66f1b7e2a1c9d4e8f0123456")
Never commit a real connection string like this to source control; load it from an environment variable (process.env.MONGODB_URI) instead.
How it works step by step
- You paste the
mongodb+srv://string intomongoshor your driver. The client performs a DNS SRV lookup on the cluster address, which returns the hostnames and ports of every current replica set member — you never hard-code individual node addresses. - The client opens a TLS connection to those nodes and runs a topology discovery handshake to determine which node is currently the primary and which are secondaries.
- Atlas’s network layer checks the source IP against your project’s Access List before MongoDB authentication even starts; a mismatch here produces a connection timeout, not an auth error.
- Once past the firewall, the server authenticates the username/password against the database users you configured in Database Access, using SCRAM by default.
- After authentication, operations route to the primary for writes and, depending on your
readPreference, to the primary or a secondary for reads. Atlas’s built-in monitoring also starts tracking every operation for the metrics dashboards in the console.
Common Mistakes
Mistake 1: Forgetting to add your IP to Network Access. A freshly created cluster has an empty Access List by default, so every connection attempt hangs and eventually times out:
MongoServerSelectionError: connection <monitor> to 34.201.10.55:27017 closed
Reason: connect ETIMEDOUT
Fix it in Atlas under Network Access by adding your current IP (or, only for quick local experiments, 0.0.0.0/0 to allow any IP — never leave that open on a production project).
Mistake 2: Special characters in the password breaking the URI. Characters like @, /, or ! in a password have special meaning inside a URI and must be percent-encoded, or the string parses incorrectly and authentication fails against the wrong host:
mongosh "mongodb+srv://dbuser:p@ss!word@cluster0.ab1cd.mongodb.net/mydb"
Corrected — encode the password with encodeURIComponent (or the equivalent) before building the string:
mongosh "mongodb+srv://dbuser:p%40ss%21word@cluster0.ab1cd.mongodb.net/mydb"
Mistake 3: Omitting the database name from the URI. Leaving off the path segment silently defaults the driver’s connection to a database called test, so writes end up somewhere you didn’t intend:
const uri = "mongodb+srv://<user>:<password>@<cluster-url>/?retryWrites=true&w=majority";
const client = new MongoClient(uri);
await client.connect();
const db = client.db(); // defaults to "test"
Corrected — put the database name in the URI and pass it explicitly to client.db() so the two always agree:
const uri = "mongodb+srv://<user>:<password>@<cluster-url>/mydb?retryWrites=true&w=majority";
const client = new MongoClient(uri);
await client.connect();
const db = client.db("mydb"); // explicit, matches the URI path
Best Practices
- Scope database users narrowly (e.g.
readWriteon one database) instead of granting anatlasAdminrole to every application user. - Never leave
0.0.0.0/0in Network Access for anything beyond a throwaway experiment; list specific IPs or use Atlas’s VPC peering / Private Endpoint for production. - Store connection strings in environment variables or a secrets manager, never in committed source code.
- Percent-encode any special characters in database user passwords, or simply avoid them by generating an auto-generated Atlas password.
- Keep
retryWrites=true&w=majority(Atlas’s defaults) unless you have a specific reason to weaken the write concern. - Use the free M0 tier for learning and prototypes, but plan a migration to a dedicated M10+ tier before relying on a project in production — M0 has resource and connection limits.
- Turn on Atlas’s built-in alerts (e.g. high connection count, disk usage) early, even on a free cluster, to build the habit.
Practice Exercises
- Create a free M0 Atlas cluster, add a database user with a strong generated password, add your current IP to Network Access, and connect successfully with
mongosh. - Load the
sample_mflixsample dataset and write a query that returns the count of movies released after the year 2000. Expect a single number back fromcountDocuments. - Write a short Node.js script using the official driver that connects to your Atlas cluster via an environment variable and inserts one document into a new
userscollection; confirm the insert by reading it back withfindOne.
Summary
- Atlas runs real MongoDB on a managed replica set; every command you already know works unchanged.
mongodb+srv://connection strings use DNS SRV discovery instead of listing individual hosts.- Network Access (IP allow list) and Database Access (users/roles) are two separate, both-required gates — a timeout points at the former, an auth error at the latter.
- Always include the database name in the URI and pass it explicitly to
client.db()to avoid silently writing intotest. - Percent-encode special characters in passwords, and keep connection strings out of source control.
- The free M0 tier is fine for learning, but production workloads should move to a dedicated tier with tighter network rules.
