Install MongoDB
Learn how to install MongoDB on Windows, macOS, and Linux. This guide covers downloading the Community Edition, choosing between MSI/Homebrew/APT, verifying the installation, and running the MongoDB server for the first time.
MongoDB is a document-oriented NoSQL database that stores data in flexible, JSON-like documents called BSON (Binary JSON). Unlike traditional relational databases with fixed table schemas, MongoDB lets you store nested objects, arrays, and heterogeneous data in a single document. It's used by companies like Google, Facebook, eBay, and Adobe for applications that need flexible data models and horizontal scaling.
Why MongoDB?
Before we install, let's understand why MongoDB is worth learning:
- Schema flexibility — No need to define columns upfront. Documents in the same collection can have different fields.
- Horizontal scaling — Built-in sharding distributes data across multiple machines automatically.
- Developer-friendly — The query language is JavaScript-based and feels natural for web developers.
- High performance — Indexes, in-memory storage engine, and efficient binary format (BSON) make reads and writes fast.
- Rich query language — Supports filters, projections, aggregation pipelines, text search, geospatial queries, and more.
Installing on macOS
The easiest way to install MongoDB on macOS is via Homebrew:
# Tap the MongoDB Homebrew formulae
brew tap mongodb/brew
# Install MongoDB Community Edition
brew install mongodb-community@7.0
# Start MongoDB as a background service
brew services start mongodb-community@7.0
# Verify it's running
mongosh
If you see the MongoDB shell prompt (test>), the installation was successful. Homebrew installs the configuration file at /usr/local/etc/mongod.conf (Intel) or /opt/homebrew/etc/mongod.conf (Apple Silicon), the data directory at /usr/local/var/mongodb, and the log file at /usr/local/var/log/mongodb/mongo.log.
Installing on Windows
Download the MSI installer from the MongoDB Download Center. Choose "Windows" as the platform and "MSI" as the package.
- Run the downloaded
.msifile - Choose Complete installation type
- Check "Install MongoDB as a Service" — this lets MongoDB start automatically with Windows
- Optionally install MongoDB Compass (the official GUI)
- Click Install and wait for completion
After installation, MongoDB is available as a Windows Service and starts automatically. Open Command Prompt or PowerShell and type:
# Verify MongoDB is installed
mongosh
If mongosh isn't recognized, you need to add MongoDB's bin directory to your system PATH (covered in the next episode).
Installing on Ubuntu/Debian
# Import the MongoDB public GPG key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
# Add the MongoDB repository
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Update packages and install
sudo apt-get update
sudo apt-get install -y mongodb-org
# Start MongoDB
sudo systemctl start mongod
# Enable MongoDB to start on boot
sudo systemctl enable mongod
# Verify
mongosh
Installing on CentOS/RHEL
# Create the repo file
sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo << 'EOF'
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
EOF
# Install MongoDB
sudo yum install -y mongodb-org
# Start and enable
sudo systemctl start mongod
sudo systemctl enable mongod
Verifying the Installation
Regardless of your operating system, verify MongoDB is working:
# Check MongoDB server status
mongosh --eval "db.runCommand({ connectionStatus: 1 })"
# Or connect interactively
mongosh
You should see output similar to:
Current Mongosh Log ID: 65a1b2c3d4e5f6a7b8c9d0e1
Connecting to: mongodb://127.0.0.1:27017
Using MongoDB: 7.0.x
Using Mongosh: 2.x.x
test>
The test> prompt means you're connected to the default test database. Type exit to disconnect.
MongoDB Components
When you install MongoDB, you get several components:
- mongod — The database server daemon. This is the core process that handles data requests, manages data access, and performs background management operations.
- mongosh — The MongoDB Shell. An interactive JavaScript shell for interacting with MongoDB from the command line.
- mongos — The sharding router. Used in sharded cluster deployments to route queries to the correct shard.
- MongoDB Compass — The official GUI for MongoDB. A visual tool for exploring data, running queries, and managing your databases.
Starting and Stopping MongoDB
# macOS (Homebrew)
brew services start mongodb-community@7.0
brew services stop mongodb-community@7.0
# Linux (systemd)
sudo systemctl start mongod
sudo systemctl stop mongod
sudo systemctl restart mongod
sudo systemctl status mongod
# Windows (PowerShell as Admin)
net start MongoDB
net stop MongoDB
What's Next
You now have MongoDB installed and running. In the next episode, we'll configure the PATH environment variable so you can run MongoDB commands from anywhere, and set up Studio 3T (formerly MongoChef) — a powerful GUI for managing your MongoDB databases visually.