iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Install / Atlas

Installing MongoDB locally for development: Docker, native install, or Atlas free tier. Pick the friction level that matches the project.

MongoDB — install

EXAMPLE
# ===== Option 1: Docker (recommended for dev) =====
docker run -d --name mongo -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=root \
  -e MONGO_INITDB_ROOT_PASSWORD=dev \
  -v mongo-data:/data/db \
  mongo:7

# Connect:
mongosh 'mongodb://root:dev@localhost:27017/'

# ===== Option 2: native install =====
# macOS:
brew tap mongodb/brew
brew install mongodb-community@7.0
brew services start mongodb-community@7.0
mongosh

# Ubuntu/Debian (paste official repo first):
# https://www.mongodb.com/docs/manual/installation/
sudo apt update && sudo apt install -y mongodb-org
sudo systemctl enable --now mongod
mongosh

# Windows: download MSI from mongodb.com; service installs automatically.

# ===== Option 3: MongoDB Atlas (managed, free M0 tier) =====
# Sign up at cloud.mongodb.com -> create free cluster -> get connection string
# Easiest for production-feeling environments without ops.

# ===== Hello, database =====
mongosh 'mongodb://localhost:27017'

use shop;
db.products.insertOne({ name: 'Widget', price: 1995 });
db.products.find();
db.products.createIndex({ name: 1 });

# ===== mongosh basics =====
show dbs
show collections
use mydb
db.coll.help()
exit

# ===== Drivers =====
# Node:     npm i mongodb       (official); or mongoose (ODM)
# Python:   pip install pymongo
# Go:       go.mongodb.org/mongo-driver
# Java:     mongodb-driver-sync

# ===== Quick health checks =====
mongosh --eval 'db.runCommand({ ping: 1 })'
mongosh --eval 'db.serverStatus().connections'

# ===== Patterns to internalise =====
# - Use Docker for ephemeral dev DBs; switch in and out of versions easily
# - Atlas free tier for hobby + side projects
# - One Mongo per environment; namespace databases per service
# - mongosh + Compass GUI for exploration

# ===== Pitfalls =====
# - Exposing port 27017 without auth on a public IP
# - Running on default unauth ports in shared dev environments
# - Storing data in the container without a volume -> wipes on rm
# - Mixing major versions (4.x driver vs 7.x server) without checking compat

Why it matters

For local Mongo: Docker for speed, native install for daily use, Atlas free tier when you want managed. mongosh is the canonical client; Compass is the GUI when you need one. Get auth, ports, and volumes right early and the rest is just data.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Local — Atlas in the cloud is the easiest path.
brew install mongodb-community     # macOS
# or sign up at mongodb.com/cloud/atlas
Try it Yourself »

Discussion

Loading…