DocsHub
Basics

Installation

Install MongoDB Server, MongoDB Compass, and mongosh on your machine — step by step.

Installation

To work with MongoDB locally, you need three things:

  • MongoDB Server — the actual database engine that runs on your machine
  • MongoDB Compass — a visual GUI tool to browse and manage your data
  • mongosh — the MongoDB shell, where you type commands and run queries

Let's install all three.


1. Install MongoDB Server

Windows

  1. Go to mongodb.com/try/download/community
  2. Select Windows, choose the latest version, and select MSI as the package
  3. Download and run the installer
  4. During installation, check "Install MongoDB as a Service" — this means MongoDB will start automatically when your computer starts
  5. Also check "Install MongoDB Compass" — this installs Compass at the same time, saving you a separate step

Once installed, MongoDB runs as a background service. You do not need to start it manually.

macOS

The easiest way on macOS is with Homebrew. If you do not have Homebrew, install it from brew.sh first.

# Add the MongoDB tap
brew tap mongodb/brew

# Install MongoDB Community Edition
brew install mongodb-community

# Start MongoDB as a background service
brew services start mongodb-community

MongoDB is now running in the background.

Linux (Ubuntu)

# Import the MongoDB public key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
  sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg

# Add the MongoDB 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

# Update and install
sudo apt-get update
sudo apt-get install -y mongodb-org

# Start MongoDB
sudo systemctl start mongod

# Enable it to start automatically on boot
sudo systemctl enable mongod

Verify MongoDB Server is running

mongod --version

You should see something like:

db version v7.0.x

2. Install MongoDB Compass

MongoDB Compass is a visual tool that lets you see your databases, browse documents, run queries, and manage indexes — all without typing commands. It is very useful when you are learning.

If you did not install it during the MongoDB Server setup on Windows, download it separately:

  1. Go to mongodb.com/try/download/compass
  2. Download the installer for your operating system
  3. Run it and follow the steps

Once installed, open Compass. You will see a connection screen like this:

New Connection
──────────────────────────────
URI: mongodb://localhost:27017
──────────────────────────────
[ Connect ]

The default connection string mongodb://localhost:27017 connects to your locally running MongoDB server. Click Connect and you are in.

Port 27017 is the default port MongoDB listens on. You will see this number a lot. Unless you changed it during installation, always use this port for local connections.


3. Install mongosh

mongosh is the MongoDB Shell — a command-line tool where you type MongoDB commands directly. Even if you prefer Compass, you should know mongosh because it is faster for quick operations and essential for working on servers.

Windows

mongosh is included when you install MongoDB Server via the MSI installer. If it was not installed:

  1. Go to mongodb.com/try/download/shell
  2. Download the Windows installer and run it

macOS

brew install mongosh

Linux

sudo apt-get install -y mongodb-mongosh

Verify mongosh is installed

mongosh --version

You should see something like:

2.x.x

4. Connect mongosh to Your Local Database

Now let's open the shell and connect to your locally running MongoDB server.

mongosh

If everything is working, you will see:

Current Mongosh Log ID: ...
Connecting to: mongodb://127.0.0.1:27017/
Using MongoDB: 7.0.x
Using Mongosh: 2.x.x

test>

You are now inside the MongoDB shell, connected to your local server. The test> prompt means you are currently using a database called test — MongoDB's default database.


5. Create the School Database

Let's create the school database we will use throughout this guide. In mongosh, run:

use school

You will see:

switched to db school

Now insert your first document to make the database real — MongoDB does not actually create a database until you put something in it:

db.students.insertOne({
  name: "Ali Hassan",
  age: 16,
  grade: "10th"
})

You should see:

{
  acknowledged: true,
  insertedId: ObjectId('64a1f2c3e4b0a1b2c3d4e5f6')
}

Your school database and students collection now exist. You just inserted your first MongoDB document.

To confirm, run:

db.students.find()

Output:

[
  {
    _id: ObjectId('64a1f2c3e4b0a1b2c3d4e5f6'),
    name: 'Ali Hassan',
    age: 16,
    grade: '10th'
  }
]

Everything is working.


Quick Summary

ToolWhat it doesHow to open
MongoDB ServerThe database engine — stores all your dataRuns automatically in background
MongoDB CompassVisual GUI to browse and manage dataOpen the Compass app
mongoshCommand-line shell to run queriesType mongosh in terminal

Keep mongosh open as you go through this guide. You will use it constantly to try out every concept we cover — the best way to learn MongoDB is to run every example yourself as you read.

On this page