MongoDB Installation & Setup Guide

Complete step-by-step guide to install and configure MongoDB on your computer. Choose between local installation or cloud setup with MongoDB Atlas.

Which Option to Choose?

You have two options to get MongoDB running:

Option 1: Local Installation

Install MongoDB directly on your computer

Pros:

  • Works offline
  • Faster for local development
  • More control over configuration

Cons:

  • Takes up disk space
  • Requires installation steps
  • Need to manage updates

Option 2: MongoDB Atlas (Cloud)

Use MongoDB in the cloud (free tier available)

Pros:

  • No installation needed
  • Access from anywhere
  • Automatic backups
  • Production-ready

Cons:

  • Requires internet connection
  • Free tier has storage limits (512MB)

Recommendation for Beginners

Start with MongoDB Atlas (Cloud) - it's easier and gets you up and running in 5 minutes. You can always install locally later if needed!

Local Installation

Follow the instructions for your operating system:

Step 1: Download MongoDB Community Server

  1. Visit mongodb.com/try/download/community
  2. Select the latest stable version (7.0 or higher)
  3. Choose your operating system
  4. Download the installer

Step 2: Install MongoDB

Windows Installation:

  1. Double-click the downloaded MSI file
  2. Click "Next" on the welcome screen
  3. Accept the license agreement
  4. Choose "Complete" installation
  5. Service Configuration Screen:
    • ✓ Keep "Install MongoDB as a Service" checked
    • ✓ Select "Run service as Network Service user"
    • Service Name: MongoDB
    • Data Directory: Keep default
    • Log Directory: Keep default
    • Click "Next"
  6. MongoDB Compass (optional):
    • Keep checked if you want a visual interface
    • Or uncheck to skip
  7. Click "Install" and wait
  8. Click "Finish"

Adding MongoDB to PATH (Windows)

  1. Search for "Environment Variables" in Windows
  2. Click "Edit the system environment variables"
  3. Click "Environment Variables" button
  4. Under "System variables", find and select Path
  5. Click "Edit"
  6. Click "New" and add:
    text
    C:\Program Files\MongoDB\Server\7.0\bin
  7. Click "OK" on all windows
  8. Restart your terminal

macOS Installation:

Using Homebrew (easiest method):

bash
# Install Homebrew if you don't have it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add MongoDB tap
brew tap mongodb/brew
# Install MongoDB
brew install mongodb-community@7.0
# Start MongoDB service
brew services start mongodb-community@7.0

Linux (Ubuntu/Debian):

bash
# Import MongoDB public 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 focal/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
MongoDB Atlas (Cloud Setup)

Follow these steps to set up a free cloud database in minutes:

Step 1: Create an Account

  1. Go to cloud.mongodb.com
  2. Click "Sign Up"
  3. Sign up with email or use Google/GitHub
  4. Verify your email if required

Step 2: Create a Free Cluster

  1. After logging in, click "Build a Database"
  2. Select M0 FREE tier (512MB storage)
  3. Choose a cloud provider (AWS, Google Cloud, or Azure)
  4. Select a region close to you
  5. Name your cluster (or keep default "Cluster0")
  6. Click "Create Cluster"

Step 3: Security Setup

You'll see a Security Quickstart screen:

  1. Create Database User:
    • Username: myuser (or your choice)
    • Password: Click "Autogenerate Secure Password"
    • SAVE THIS PASSWORD! Copy it somewhere safe
    • Click "Create User"
  2. Network Access:
    • Choose "My Local Environment"
    • Click "Add My Current IP Address"
    • Or click "Allow Access from Anywhere" (0.0.0.0/0) for learning
    • Click "Finish and Close"

What if I see "Service Config" screen?

Sometimes after clicking "Finish and Close", you might see additional options:

  • Deployment Type: Leave as "Cluster"
  • Cloud Provider & Region: Already set
  • Cluster Tier: Should show "M0 Sandbox (Free)"
  • Additional Settings: Leave everything as default
  • Just click "Create Cluster" or "Confirm"

Wait 3-5 minutes for your cluster to deploy.

Step 4: Get Your Connection String

  1. Once cluster is ready, click "Connect" button
  2. Select "Connect your application"
  3. Driver: Choose "Node.js"
  4. Version: Select latest
  5. Copy the connection string
  6. Replace <password> with your actual password
  7. Replace myFirstDatabase with your database name (e.g., schoolDB)

Example connection string:

text
mongodb+srv://myuser:mypassword@cluster0.xxxxx.mongodb.net/schoolDB?retryWrites=true&w=majority

Success!

Your MongoDB Atlas cluster is ready! You can now connect to it from your applications using the connection string.

Verify Installation

Let's make sure everything is working correctly:

For Local Installation:

Open your terminal and run:

bash
# Check MongoDB Shell version
mongosh --version
# Connect to local MongoDB
mongosh
# You should see a connection message and a prompt like:
# test>

If you see the MongoDB shell prompt, congratulations! MongoDB is installed correctly.

For MongoDB Atlas:

Test your connection using mongosh:

bash
# Connect to your Atlas cluster (replace with your connection string)
mongosh "mongodb+srv://myuser:mypassword@cluster0.xxxxx.mongodb.net/schoolDB"
# You should see a connection success message

Quick Test:

Once connected, try these commands:

javascript
// Show all databases
show dbs
// Create a test database
use testDB
// Insert a test document
db.test.insertOne({ message: "Hello MongoDB!" })
// Find the document
db.test.find()
// You should see your inserted document!

All Set!

Now proceed to the MongoDB Basics lesson to start learning how to use MongoDB!

Built for learning — keep experimenting!