Installation and mongosh
Introduction
Before you insert a document, you need a running MongoDB server and the mongosh shell to talk to it. This chapter covers Docker (recommended for development), native installs on Linux and macOS, Windows via WSL, and your first commands—use, show dbs, and ping. By the end, local MongoDB answers on port 27017.
Prerequisites
- A terminal (Linux, macOS, or Windows with WSL2)
- What Is MongoDB — terminology
- Docker Desktop optional but recommended
Install With Docker (Recommended)
Docker gives a consistent MongoDB 7 instance without touching system packages.
# Pull the official image
docker pull mongo:7
# Run with a named volume so data survives container restarts
docker run -d \
--name mongodb \
-p 27017:27017 \
-v mongo_data:/data/db \
mongo:7Verify the container:
docker ps
docker logs mongodb --tail 20Connect with mongosh inside the container:
docker exec -it mongodb mongoshOr install mongosh on your host and connect to localhost:
mongosh "mongodb://127.0.0.1:27017"Tip
Persist Data
The mongo_data volume keeps databases across docker rm. To wipe dev data: docker volume rm mongo_data (destructive).
Docker Compose (Optional)
docker-compose.yml:
services:
mongodb:
image: mongo:7
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
volumes:
mongo_data:docker compose up -dLinux (Ubuntu / Debian)
Official MongoDB packages:
# Import MongoDB public GPG key and add repo (see mongodb.com/docs for your distro version)
# Simplified: many teams use Docker instead for dev parity
sudo apt update
sudo apt install -y mongodb-mongosh
# If mongod is installed via package manager:
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongodPing from mongosh:
mongosh "mongodb://127.0.0.1:27017" --eval 'db.runCommand({ ping: 1 })'Expected: { ok: 1 }.
macOS (Homebrew)
brew tap mongodb/brew
brew install mongodb-community@7.0
brew install mongosh
# Start as a service
brew services start mongodb-community@7.0
# Or run in foreground for debugging
mongod --config /opt/homebrew/etc/mongod.confConnect:
mongoshWindows
Native mongod on Windows is supported via MSI installer from MongoDB.com. For consistency with Linux deploys, many teams use WSL2 + Docker:
wsl --installInside WSL Ubuntu, follow the Docker steps above.
Install mongosh on Windows from the MongoDB download center if you need a host-side shell.
MongoDB Atlas (Cloud Preview)
For cloud-hosted dev:
- Create a free M0 cluster at mongodb.com/atlas
- Add your IP to Network Access
- Create a database user
- Copy the connection string:
mongodb+srv://user:pass@cluster.mongodb.net/
mongosh "mongodb+srv://USER:PASSWORD@cluster0.example.mongodb.net/mydb"Full Atlas workflow — MongoDB Atlas.
mongosh Basics
Launch interactive shell:
mongosh
# or with explicit URI
mongosh "mongodb://127.0.0.1:27017"Essential commands
Code explanation:
use mydbswitches context; the database file is created when you first store datashow dbssize reflects data files on disk, not empty switched databases
Run one-liners from the terminal
mongosh --quiet --eval 'db.runCommand({ ping: 1 })'
mongosh hello_demo --eval 'db.stats()'Run a script file
seed.js:
// seed.js — run with: mongosh hello_demo seed.js
db.items.insertOne({ name: "First item", createdAt: new Date() })
print("Seeded one item")mongosh hello_demo seed.jsConnection URI Format
mongodb://[username:password@]host1[:port1][,...]/[database][?options]
mongodb+srv://user:pass@cluster.example.mongodb.net/mydb?retryWrites=true&w=majority| Part | Example |
|---|---|
| Host | 127.0.0.1:27017 |
| Auth | admin:secret@ |
| Database | /hello_demo |
| Options | ?authSource=admin |
URL-encode special characters in passwords.
GUI: MongoDB Compass
MongoDB Compass provides visual browsing, schema sampling, and index management. Connect with the same URI as mongosh. Compass complements—not replaces—the shell for learning.
Common Problems
| Symptom | Fix |
|---|---|
| Connection refused | Start mongod or Docker container; check port 27017 |
mongosh: command not found | Install mongosh package separately on some distros |
| Docker port in use | Stop other MongoDB or map -p 27018:27017 |
| Atlas timeout | IP whitelist, wrong password, firewall |
Empty show dbs after use | Normal until first insert |
Post-Chapter Checklist
- MongoDB runs locally (Docker or native)
-
mongoshconnects andpingreturnsok: 1 - You can
usea database andshow collections - You know default port 27017 and URI format
FAQ
mongo vs mongosh?
Legacy mongo shell is removed in MongoDB 6+. Use mongosh only.
Do I need auth for local dev?
Single-node local dev often runs without auth. Enable users before exposing to a network—Security.
How much RAM does MongoDB need?
Dev: 512 MB–1 GB container limit is fine. Production sizing depends on working set—Performance.
Can I run MongoDB and MySQL together?
Yes—common pattern: MySQL for billing, MongoDB for content. Different ports (27017 vs 3306).
Upgrade MongoDB version?
Read release notes; backup with mongodump before major upgrades.