This guide walks you through installing MongoDB Community Server on your machine, starting the mongod service, connecting with mongosh, and running your first database commands.
01
Community Server
Free local database.
02
Windows / Mac / Linux
Platform install steps.
03
mongosh shell
Connect and run commands.
04
Verify setup
ping test & first query.
05
Local vs Atlas
Pick the right option.
06
Troubleshooting
Fix common connection errors.
Fundamentals
Definition and Usage
MongoDB is a document database. To use it locally you install MongoDB Community Server—the open-source edition that runs the mongod process on your computer. You then connect with mongosh, MongoDB’s modern shell, to create databases, insert documents, and run aggregations.
💡
Beginner tip
Think of mongod as the engine and mongosh as the steering wheel. The server must be running before the shell can connect to mongodb://localhost:27017.
After installation you can follow MongoDB tutorials on CodeToFun—starting with aggregation operators and accumulators—using the same local instance.
Planning
🚀 Choose your setup path
Option
Best for
Install effort
Community Server (local)
Learning, offline dev, full control
Medium — this guide
MongoDB Atlas (cloud)
Quick start, teams, no local service
Low — free tier at mongodb.com/atlas
Docker
CI, isolated versions, containers
Medium — docker run mongo
This tutorial focuses on local Community Server installation—the foundation most beginners want before exploring Atlas or Docker.
Reference
🧰 System requirements
Before downloading, confirm your machine meets MongoDB’s basics:
64-bit OSRequired
Windows 10+, macOS 11+, or a supported Linux distribution (Ubuntu LTS, Debian, RHEL).
RAM4 GB+
4 GB minimum for learning; 8 GB or more recommended for larger datasets and aggregation labs.
disk space10 GB+
Leave room for data files, logs, and future indexes under the default dbPath.
port 27017Default
Ensure nothing else binds to TCP 27017 unless you configure a custom port in mongod.conf.
Cheat Sheet
⚡ Quick Reference
Task
Command / action
Connect (default)
mongosh
Health check
db.runCommand({ ping: 1 })
Start (Linux)
sudo systemctl start mongod
Start (macOS Homebrew)
brew services start mongodb-community
Default port
27017
Stop shell
exit or Ctrl+C twice
Windows
MSI installer
+ MongoDB Service
Official .msi from mongodb.com
macOS
brew tap mongodb/brew
brew install mongodb-community
Homebrew tap method
Ubuntu
apt install
mongodb-org
After adding MongoDB repo
Verify
db.runCommand({ ping: 1 })
Expect ok: 1
Hands-On
Examples Gallery
Install steps for each platform, then verify and run your first commands in mongosh.
💻 Platform installs
Download MongoDB Community Server from the official download page, then follow your OS steps below.
Example 1 — Install on Windows
Download the Windows MSI for MongoDB Community Server.
Run the installer. Choose Complete setup.
Check Install MongoDB as a Service so mongod starts automatically.
Install mongosh when prompted (or install separately from the same download page).
Optional: install MongoDB Compass for a graphical UI.
PowerShell
# Confirm mongosh is on PATH
mongosh --version
# Connect to local server
mongosh
How It Works
The MSI registers a Windows Service named MongoDB. It listens on port 27017 and stores data under C:\Program Files\MongoDB\Server\{version}\data by default.
Example 2 — Install on macOS (Homebrew)
Terminal
brew tap mongodb/brew
brew install mongodb-community
# Start the server as a background service
brew services start mongodb-community
# Connect
mongosh
📤 mongosh prompt:
Current Mongosh Log ID: …
Connecting to: mongodb://127.0.0.1:27017
test>
How It Works
Homebrew installs binaries under its prefix and manages the mongod service with brew services. Data defaults to /opt/homebrew/var/mongodb on Apple Silicon.
Example 3 — Install on Ubuntu / Debian
Import MongoDB’s GPG key and add the official repository (see current docs for your version), then install:
use switches databases (MongoDB creates it on first write). insertOne adds a BSON document; findOne reads it back—your install is fully working.
Compare
📋 Local install vs other options
Approach
Pros
Cons
Community Server (local)
Full control, offline, learn mongod internals
You manage updates and disk space
MongoDB Atlas
Free tier, backups, no local service
Requires internet; cluster limits on free tier
Docker
Reproducible version, easy tear-down
Volume mounts and networking to learn
MongoDB Compass only
GUI for browsing data
Still needs a running mongod or Atlas URI
🧠 How a local MongoDB install works
1
mongod starts
The server process binds to a port (default 27017) and opens the data directory on disk.
Server
2
mongosh connects
The shell sends wire-protocol commands over TCP to localhost:27017.
Client
3
Commands execute
Inserts, finds, and aggregations run against databases and collections stored as BSON files.
Query
=
✅
Ready for tutorials
Your local instance is ready for operators, accumulators, and aggregation labs on CodeToFun.
Important
📝 Notes
Default local installs have no authentication—fine for learning on your machine; enable auth before exposing to a network.
Add the MongoDB bin folder to your PATH if mongosh is not recognized after install.
On Windows, use Services (services.msc) to restart MongoDB if the shell cannot connect.
Check logs when startup fails: Windows Event Viewer, macOS Homebrew logs, Linux /var/log/mongodb/mongod.log.
MongoDB Compass is optional—it connects to the same URI as mongosh for visual browsing.
Next topic: $add operator (aggregation expressions).
Wrap Up
Conclusion
Installing MongoDB locally means running mongod, connecting with mongosh, and confirming { ok: 1 } from a ping command. Pick the installer for your OS, start the service, and run a sample insert.
From here, explore aggregation tutorials—operators and accumulators—on your new instance, or spin up MongoDB Atlas if you prefer a managed cloud database.
Install mongosh alongside Community Server for the modern shell experience
Run db.runCommand({ ping: 1 }) after every install or upgrade
Keep MongoDB updated to a supported LTS release from mongodb.com
Use a dedicated data disk or folder with enough free space for indexes
Document your connection URI if you switch between local and Atlas
❌ Don’t
Expose an unauthenticated mongod to the public internet
Delete the data directory while mongod is running—stop the service first
Mix very old shell tools (legacy mongo) with new server versions without checking compatibility
Skip reading official platform notes for your exact OS version
Assume Compass alone installs the database—it is only a client
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about installation
Use this checklist whenever you set up MongoDB on a new machine.
5
Core concepts
⚙01
mongod + mongosh
Server and shell.
Basics
💻02
OS installers
MSI, Homebrew, apt.
Platform
🔌03
Port 27017
Default endpoint.
Network
✅04
ping test
Verify connection.
Check
☁05
Atlas option
Cloud alternative.
Choice
❓ Frequently Asked Questions
Install MongoDB Community Server (the mongod database process) and mongosh (the modern shell). On Windows the official MSI can install both. macOS users often use Homebrew; Linux users add MongoDB's apt repository.
mongod is the database server that stores data and listens on port 27017 by default. mongosh is the client you type commands into—it connects to mongod to run queries and admin tasks.
Open mongosh and run db.runCommand({ ping: 1 }). A response like { ok: 1 } means the server accepted the connection. On Linux you can also run sudo systemctl status mongod.
Atlas is MongoDB's free cloud tier—no local service to manage, great for tutorials and team sharing. Local Community Server is best when you need offline access, custom configs, or to learn how mongod works under the hood.
Default paths vary by OS: on Linux typically /var/lib/mongodb, on macOS Homebrew installs under /opt/homebrew/var/mongodb, on Windows under C:\Program Files\MongoDB\Server\{version}\data. You can change dbPath in mongod.conf.
mongod may not be started, may be bound to a different port, or a firewall may block the connection. Start the service first (Windows Service, brew services, or systemctl) then retry.
Did you know?
The legacy mongo shell was replaced by mongosh, which supports modern JavaScript syntax, improved autocomplete, and the same API for Atlas and local servers. If tutorials mention “the MongoDB shell,” they mean mongosh today. See the official installation docs.