Installing MongoDB
Setting up MongoDB locally and connecting with mongosh
Skills taught: mongodb.setup
Three Ways to Get MongoDB
- MongoDB Atlas (Cloud) — Free tier, zero install, runs in the browser. Best for learning.
- Local Install — Full control, works offline. Good for active development.
- Docker — Run MongoDB in a container. Best for CI/CD and reproducible environments.
Option 1: MongoDB Atlas (Recommended for Beginners)
MongoDB Atlas is the cloud-hosted version of MongoDB. The M0 free tier gives you 512 MB of storage and a fully managed 3-node replica set at no cost. No credit card required.
- Go to mongodb.com/cloud/atlas and sign up
- Create a free M0 cluster (choose any cloud provider and region)
- Add your IP address to the IP Access List
- Create a database user with a username and password
- Click 'Connect' and copy your connection string
Option 2: Install Locally
# macOS (Homebrew)
brew tap mongodb/brew
brew install mongodb-community@7.0
brew services start mongodb-community@7.0
# Ubuntu / Debian Linux
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update && sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod # auto-start on boot
# Windows: Download the .msi installer from
# https://www.mongodb.com/try/download/community
Option 3: Docker (Quickest for Dev)
# Start MongoDB in Docker with persistent data
docker run -d \
--name mongodb \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
-v mongodb_data:/data/db \
mongo:7.0
# Verify it's running
docker ps
# Stop and remove
docker stop mongodb && docker rm mongodb
Using mongosh (the MongoDB Shell)
mongosh is the official MongoDB Shell — a modern, Node.js-based REPL that supports full JavaScript including async/await. Install it separately from the MongoDB server.
# Install mongosh
npm install -g mongosh # via npm
brew install mongosh # macOS
# Connect to local MongoDB
mongosh
# or explicitly
mongosh "mongodb://localhost:27017"
# Connect to Atlas
mongosh "mongodb+srv://username:password@cluster.mongodb.net/mydb"
# Connect to Docker MongoDB
mongosh "mongodb://admin:password@localhost:27017" --authenticationDatabase admin
// Inside mongosh — these are JavaScript commands
// List all databases
show dbs
// Switch to (or create) a database
use myapp
// Show current database
db
// List collections in current database
show collections
// Quick test insert
db.test.insertOne({ hello: 'world' })
// Find it back
db.test.findOne()
Tip: mongosh is a full JavaScript environment — you can use variables, loops, and async/await. Try: const result = await db.users.findOne(); console.log(result.name)
Warning: On a fresh local install, MongoDB has NO authentication enabled. Never deploy to production without enabling authentication. See the Security lesson for how to secure MongoDB.
Frequently Asked Questions
How do I connect to Atlas from mongosh?
Use mongosh with the connection string from the Atlas Connect dialog, e.g. mongosh "mongodb+srv://user:pass@cluster.mongodb.net/mydb". You can find this string in your Atlas cluster's 'Connect' dialog under 'Connect with the MongoDB Shell'.
What is the difference between mongod and mongosh?
mongod is the MongoDB database server process — it manages data files and listens for connections. mongosh is the client shell you use to send commands to mongod. You need mongod running before you can connect with mongosh.
Should I use MongoDB Atlas or install locally?
For learning and small projects, Atlas free tier (M0) is the easiest path — no installation, automatic backups, and free forever. For production, Atlas provides managed infrastructure. Install locally only when you need offline access or OS-level control over the database.