Installation & Atlas Setup
You can run MongoDB in two ways:
- MongoDB Atlas: Fully managed cloud service (Recommended for beginners & production).
- Local / Docker: Self-hosted on your machine.
1. MongoDB Atlas (Free Cloud Tier)
The easiest way to start is using the free tier on Atlas.
- Sign Up: Go to MongoDB Atlas and create an account.
- Create Cluster: Select Build a Database → M0 Sandbox (Free) → Select a region close to you → Create.
- Security Setup:
- Database User: Create a username (e.g.,
admin) and a strong password. Save this password! - Network Access: Add your current IP address (or
0.0.0.0/0to allow all, but be careful).
- Database User: Create a username (e.g.,
- Connect: Click Connect → Drivers to get your connection string.
2. Local Installation via Docker
If you prefer running locally, Docker is the cleanest method.
Quick Start (One-Liner)
docker run -d \
--name mongodb \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password123 \
mongo:latest
Production-Ready docker-compose.yml
This setup includes Mongo Express, a web-based admin interface.
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password123
volumes:
- mongo-data:/data/db
restart: unless-stopped
mongo-express:
image: mongo-express
container_name: mongo-express
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password123
ME_CONFIG_MONGODB_URL: mongodb://admin:password123@mongodb:27017/
depends_on:
- mongodb
restart: unless-stopped
volumes:
mongo-data:
Run with docker-compose up -d and visit http://localhost:8081.
3. The Connection String
The Connection String is the URL your application uses to find and authenticate with the database.
Interactive: Connection String Builder
Construct a valid URI by adjusting the parameters below.
Generated Connection String
mongodb://admin:password123@localhost:27017/?retryWrites=true&w=majority
Connection Options Explained
Why add ?w=majority?
- w=majority (Write Concern): This ensures that a write is acknowledged by the majority of nodes in the Replica Set (e.g., 2 out of 3) before returning success to the application. This prevents data loss if the Primary crashes immediately after a write.
- retryWrites=true: If a network glitch occurs during a write, the driver will automatically retry the operation once.
- readPreference: Controls where your app reads from.
primary: (Default) Always read from Primary (Strong Consistency).secondaryPreferred: Read from Secondary if available (Eventual Consistency, better performance).
4. Verification
Once your database is running, verify the connection using mongosh (MongoDB Shell) or a GUI like Compass.
# Connect to local
mongosh "mongodb://admin:password123@localhost:27017/"
# Expected Output
# Current Mongosh Log ID: ...
# Connecting to: mongodb://admin:password123@localhost:27017/
# ...
# test>