MongoDB
Installing MongoDB
Before writing a single query you need MongoDB running on your machine. This lesson walks through installing MongoDB Community Server on Windows, macOS, and Linux, verifying the installation, connecting with the shell, and setting up the course dataset you will use for every hands-on lesson from here onwards.
Choosing Your Installation Method
MongoDB can be installed locally on your own machine or accessed instantly through the cloud. For this course either approach works — choose based on your preference.
- Local install — MongoDB Community Server runs on your machine. Full control, works offline, best for understanding the internals.
- MongoDB Atlas (cloud) — free M0 cluster available in minutes, no installation needed. Best if you want to skip local setup entirely.
The official MongoDB documentation covers every platform and version. Always use it as your definitive reference — click below to open the installation guide directly.
1. Installing on Windows
MongoDB provides an MSI installer for Windows that sets up the server, optional tools, and can install MongoDB as a Windows Service so it starts automatically on boot.
# Windows installation steps
steps = [
"1. Go to mongodb.com/try/download/community",
"2. Select: Version = Latest | Platform = Windows | Package = MSI",
"3. Run the downloaded .msi installer",
"4. Choose 'Complete' setup type",
"5. Check 'Install MongoDB as a Service' — auto-starts on boot",
"6. Optionally install MongoDB Compass (GUI) during setup",
"7. Ensure the bin folder is on your PATH:",
" C:\\Program Files\\MongoDB\\Server\\7.0\\bin",
"8. Open a new terminal and verify:",
" mongosh --version",
]
for step in steps:
print(step)2. Select: Version = Latest | Platform = Windows | Package = MSI
3. Run the downloaded .msi installer
4. Choose 'Complete' setup type
5. Check 'Install MongoDB as a Service' — auto-starts on boot
6. Optionally install MongoDB Compass (GUI) during setup
7. Ensure the bin folder is on your PATH:
C:\Program Files\MongoDB\Server\7.0\bin
8. Open a new terminal and verify:
mongosh --version
2. Installing on macOS
The recommended method on macOS is Homebrew — the standard macOS package manager. If you do not have Homebrew, install it first from brew.sh.
# macOS — install via Homebrew (run in Terminal)
brew tap mongodb/brew
brew update
brew install mongodb-community@7.0
# Start MongoDB as a background service
brew services start mongodb-community@7.0
# Confirm it is running
brew services list
# Connect with the shell
mongosh==> Installing mongodb-community@7.0
...
Name Status User File
mongodb-community@7.0 started root ~/Library/LaunchAgents/homebrew.mxcl.mongodb-community@7.0.plist
3. Installing on Linux (Ubuntu / Debian)
MongoDB provides an official APT repository for Ubuntu and Debian. The commands below install the latest stable release.
# Ubuntu / Debian — install via APT (run in terminal)
# 1. Import the MongoDB 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
# 2. Add the MongoDB APT repository
echo "deb [ arch=amd64,arm64 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
# 3. Update package list and install
sudo apt-get update
sudo apt-get install -y mongodb-org
# 4. Start and enable the service
sudo systemctl start mongod
sudo systemctl enable mongod
# 5. Check the service is active
sudo systemctl status mongodLoaded: loaded (/lib/systemd/system/mongod.service)
Active: active (running) since Mon 2024-01-15 09:30:00 UTC
Main PID: 1234 (mongod)
systemctl start mongodstarts the server now;systemctl enable mongodmakes it start on every reboot- For Red Hat / CentOS, replace
apt-getwithyumand use the RHEL repository URL from the official docs
Verifying the Installation
Once installed, connect using mongosh — the modern MongoDB interactive shell. A successful connection confirms everything is working.
# Verify the install — connect and run basic commands
# In your terminal:
mongosh
# Inside mongosh:
db.version() # prints MongoDB server version
show dbs # lists all databases
use test # switch to a database
db.hello.insertOne({ msg: "it works" }) # insert a test document
db.hello.findOne() # read it back
db.hello.drop() # clean upConnecting to: mongodb://127.0.0.1:27017/
Using MongoDB: 7.0.5
Using Mongosh: 2.1.1
test> db.version()
7.0.5
test> show dbs
admin 40.00 KiB
config 72.00 KiB
local 40.00 KiB
test> db.hello.insertOne({ msg: "it works" })
{ acknowledged: true, insertedId: ObjectId('64a1f2e3...') }
test> db.hello.findOne()
{ _id: ObjectId('64a1f2e3...'), msg: 'it works' }
mongoshwith no arguments connects tolocalhost:27017— the default for a local install- The prompt shows the current database name —
test>is the default starting database - To connect to Atlas:
mongosh "mongodb+srv://cluster0.xxxxx.mongodb.net" --username yourUser
Installing the Python Driver — PyMongo
All Python-based examples in this course use PyMongo — the official MongoDB Python driver. Install it with pip before moving to the next lesson.
# Install PyMongo (run in your terminal)
pip install "pymongo[srv]"
# [srv] adds DNS SRV support needed for Atlas connection strings
# Works for local connections too — always use this variant
# Verify in Python
import pymongo
from pymongo import MongoClient
print("PyMongo version:", pymongo.version)
client = MongoClient("mongodb://localhost:27017/")
print("MongoDB server version:", client.server_info()["version"])
client.close()MongoDB server version: 7.0.5
- Always use
pymongo[srv]— it includes DNS SRV support required for Atlas connection strings MongoClientis the entry point for all PyMongo operations — one client per application
The Course Dataset — Dataplexa Store
From this lesson onwards every hands-on example uses the same shared dataset — the Dataplexa Store. It is a realistic e-commerce dataset with four collections: users, products, orders, and reviews. It has been designed to demonstrate every MongoDB feature covered across all 40 lessons — from basic CRUD to aggregation pipelines, indexes, transactions, and more.
Download the dataset file below, then follow the import steps to load it into your local MongoDB or Atlas cluster.
The dataset contains:
- users — 5 users across UK, Spain, USA, Germany — with membership tiers, join dates, and tags
- products — 7 products across Electronics, Stationery, and Furniture — with price, stock, and rating
- orders — 7 orders with embedded line items, statuses (delivered, shipped, processing, cancelled), and totals
- reviews — 5 product reviews linked to users and products by ID
# Import the dataset — run these in your terminal
# Import all four collections into the 'dataplexa' database
mongoimport --db dataplexa --collection users --file dataplexa_store.json --jsonArray
mongoimport --db dataplexa --collection products --file dataplexa_store.json --jsonArray
mongoimport --db dataplexa --collection orders --file dataplexa_store.json --jsonArray
mongoimport --db dataplexa --collection reviews --file dataplexa_store.json --jsonArray
# Verify inside mongosh
use dataplexa
show collections
db.users.countDocuments()
db.products.countDocuments()
db.orders.countDocuments()
db.reviews.countDocuments()7 document(s) imported successfully into products
7 document(s) imported successfully into orders
5 document(s) imported successfully into reviews
dataplexa> show collections
orders
products
reviews
users
dataplexa> db.users.countDocuments()
5
dataplexa> db.products.countDocuments()
7
dataplexa> db.orders.countDocuments()
7
dataplexa> db.reviews.countDocuments()
5
- Import the dataset once — it persists on disk until you explicitly drop it
- All lessons from here on reference
db.users,db.products,db.orders, anddb.reviews - If you ever need a clean slate:
use dataplexa→db.dropDatabase()→ re-import
Summary Table
| Platform | Install Method | Start Command |
|---|---|---|
| Windows | MSI installer from mongodb.com | Runs as a Windows Service automatically |
| macOS | brew install mongodb-community@7.0 |
brew services start mongodb-community@7.0 |
| Ubuntu / Debian | APT repo + apt-get install mongodb-org |
sudo systemctl start mongod |
| Cloud | MongoDB Atlas free tier — no install | Always on — connect via Atlas URI |
| Python driver | pip install "pymongo[srv]" |
Official MongoDB Python driver |
Practice Questions
Practice 1. What command connects to a local MongoDB instance using the modern shell?
Practice 2. What Linux command makes mongod start automatically on every system reboot?
Practice 3. What is the name of the database created when you import the course dataset?
Practice 4. What are the four collections in the Dataplexa Store dataset?
Practice 5. What pip install flag adds DNS SRV support for MongoDB Atlas connection strings?
Quiz
Quiz 1. What is the recommended MongoDB installation method on macOS?
Quiz 2. What does use dataplexa do in mongosh when the database does not yet exist?
Quiz 3. Which terminal tool is used to import JSON files into MongoDB collections?
Quiz 4. What default port does mongod listen on?
Quiz 5. How do you reset the course dataset to a clean state if you want to start over?
Next up — MongoDB Atlas Overview: creating a free cloud cluster and connecting from anywhere in minutes.