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
- Go to mongodb.com/try/download/community
- Select Windows, choose the latest version, and select MSI as the package
- Download and run the installer
- During installation, check "Install MongoDB as a Service" — this means MongoDB will start automatically when your computer starts
- 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-communityMongoDB 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 mongodVerify MongoDB Server is running
mongod --versionYou should see something like:
db version v7.0.x2. 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:
- Go to mongodb.com/try/download/compass
- Download the installer for your operating system
- 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:
- Go to mongodb.com/try/download/shell
- Download the Windows installer and run it
macOS
brew install mongoshLinux
sudo apt-get install -y mongodb-mongoshVerify mongosh is installed
mongosh --versionYou should see something like:
2.x.x4. Connect mongosh to Your Local Database
Now let's open the shell and connect to your locally running MongoDB server.
mongoshIf 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 schoolYou will see:
switched to db schoolNow 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
| Tool | What it does | How to open |
|---|---|---|
| MongoDB Server | The database engine — stores all your data | Runs automatically in background |
| MongoDB Compass | Visual GUI to browse and manage data | Open the Compass app |
| mongosh | Command-line shell to run queries | Type 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.