This guide will help you set up your environment to follow along with the MongoDB training materials.
Table of Contents
Prerequisites
Before you begin, ensure you have:
- A computer with internet access
- Administrator/sudo access (for installation)
- Basic command-line knowledge
- Text editor or IDE (VS Code, PyCharm, etc.)
MongoDB Installation
macOS Installation
Option 1: Using Homebrew (Recommended)
# Install Homebrew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add MongoDB tap
brew tap mongodb/brew
# Install MongoDB Community Edition
brew install mongodb-community
# Start MongoDB service
brew services start mongodb-community
# Verify installation
mongosh --version
Option 2: Manual Installation
- Download MongoDB Community Server from mongodb.com
- Extract the archive
- Move to
/usr/local/mongodb - Add to PATH in
~/.zshrcor~/.bash_profile:bash export PATH="/usr/local/mongodb/bin:$PATH" - Create data directory:
bash sudo mkdir -p /data/db sudo chown $(whoami) /data/db - Start MongoDB:
bash mongod
Linux Installation (Ubuntu/Debian)
# Import MongoDB public GPG key
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
# Add MongoDB repository
echo "deb [ arch=amd64,arm64 ] 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 package list
sudo apt-get update
# Install MongoDB
sudo apt-get install -y mongodb-org
# Start MongoDB service
sudo systemctl start mongod
# Enable MongoDB to start on boot
sudo systemctl enable mongod
# Verify installation
mongosh --version
Windows Installation
-
Download MongoDB: - Visit MongoDB Download Center - Select Windows version - Download the MSI installer
-
Run Installer: - Run the downloaded MSI file - Choose "Complete" installation - Select "Install MongoDB as a Service" - Choose "Run service as Network Service user" - Install MongoDB Compass (optional GUI tool)
-
Verify Installation: - Open Command Prompt or PowerShell - Run:
mongosh --version -
Start MongoDB: - MongoDB should start automatically as a Windows service - If not, go to Services and start "MongoDB" service
Docker Installation (All Platforms)
# Pull MongoDB image
docker pull mongo:latest
# Run MongoDB container
docker run -d \
--name mongodb \
-p 27017:27017 \
-v mongodb_data:/data/db \
mongo:latest
# Connect to MongoDB
docker exec -it mongodb mongosh
Python Setup
1. Install Python
macOS
# Check if Python is installed
python3 --version
# If not installed, install via Homebrew
brew install python3
Linux
# Install Python 3
sudo apt-get update
sudo apt-get install python3 python3-pip
Windows
- Download Python from python.org
- Run installer
- Check "Add Python to PATH" during installation
- Verify:
python --version
2. Install Python Dependencies
# Navigate to project directory
cd /path/to/NonSQL
# Install required packages
pip install -r python-examples/requirements.txt
# Or install individually
pip install pymongo python-dotenv
3. Verify Python Installation
# Check Python version (should be 3.7+)
python3 --version
# Check pip
pip3 --version
# Test PyMongo import
python3 -c "import pymongo; print(pymongo.__version__)"
Verification
1. Verify MongoDB Installation
# Check MongoDB version
mongosh --version
# Connect to MongoDB
mongosh
# In mongosh, run:
db.adminCommand('ping')
# Should return: { ok: 1 }
# Exit mongosh
exit
2. Verify Python Setup
# Run basic connection test
cd python-examples
python3 01_basic_connection.py
# Expected output:
# ✓ Successfully connected to MongoDB!
# ✓ Using database: testdb
3. Test Complete Workflow
# 1. Create some data
python3 02_create_operations.py
# 2. Read the data
python3 03_read_operations.py
# 3. Update the data
python3 04_update_operations.py
# 4. Delete some data
python3 05_delete_operations.py
Quick Start
Step 1: Start MongoDB
macOS/Linux:
# If using Homebrew
brew services start mongodb-community
# Or manually
mongod
Windows: - MongoDB should run as a service automatically - Or start from Services panel
Docker:
docker start mongodb
Step 2: Connect to MongoDB Shell
mongosh
Step 3: Try Basic Commands
// In mongosh
show dbs
use testdb
db.users.insertOne({name: "Test User", age: 25})
db.users.find()
Step 4: Run Python Examples
cd python-examples
python3 01_basic_connection.py
Troubleshooting
MongoDB Issues
Issue: "mongosh: command not found"
Solution:
- Verify MongoDB is installed: which mongosh
- Add MongoDB to PATH
- Reinstall MongoDB
Issue: "Connection refused"
Solution:
# Check if MongoDB is running
ps aux | grep mongod
# Start MongoDB
brew services start mongodb-community # macOS
sudo systemctl start mongod # Linux
Issue: "Permission denied" for /data/db
Solution:
# Create directory and set permissions
sudo mkdir -p /data/db
sudo chown $(whoami) /data/db
Issue: Port 27017 already in use
Solution:
# Find process using port 27017
lsof -i :27017
# Kill the process or use different port
mongod --port 27018
Python Issues
Issue: "ModuleNotFoundError: No module named 'pymongo'"
Solution:
# Install pymongo
pip3 install pymongo
# Or use virtual environment
python3 -m venv venv
source venv/bin/activate # macOS/Linux
# or
venv\Scripts\activate # Windows
pip install pymongo
Issue: "ConnectionFailure" in Python
Solution: 1. Verify MongoDB is running 2. Check connection string 3. Verify firewall settings 4. Check MongoDB logs
Issue: Python version too old
Solution:
# Check Python version
python3 --version
# Should be 3.7 or higher
# Update Python if needed
General Issues
Issue: Can't connect to MongoDB Atlas
Solution: 1. Check connection string 2. Verify IP whitelist in Atlas 3. Check username/password 4. Verify network connectivity
Issue: Authentication failed
Solution: 1. Verify username and password 2. Check authentication database 3. Ensure user has proper roles
Next Steps
After completing setup:
- Read the main README.md - Overview of NoSQL and MongoDB
- Study mongosh-examples.md - Learn MongoDB shell commands
- Work through Python examples - Start with
01_basic_connection.py - Practice with exercises - Try the exercises in each guide
- Build a project - Apply what you've learned
Additional Resources
Getting Help
If you encounter issues:
- Check the troubleshooting section above
- Review MongoDB logs:
/var/log/mongodb/mongod.log(Linux) or check system logs - Search MongoDB documentation
- Ask on MongoDB community forums
- Check GitHub issues for PyMongo
Setup Complete! You're ready to start learning MongoDB! 🚀