MongoDB Installation and Setup
Getting MongoDB running on a system is the first practical step toward working with it. MongoDB can be installed on Windows, macOS, and Linux. There is also a browser-based cloud option called MongoDB Atlas that requires no installation at all. This topic covers both approaches so learners can choose based on their needs.
Option 1 — Using MongoDB Atlas (Recommended for Beginners)
MongoDB Atlas is the official cloud-hosted version of MongoDB. It runs entirely in the browser and requires no downloads or configurations. This is the fastest way to get started.
Steps to Set Up MongoDB Atlas
- Go to https://www.mongodb.com/cloud/atlas and click Try Free.
- Sign up with an email address or a Google account.
- Choose the Free Tier (M0) cluster — this gives a free shared cluster with 512 MB of storage.
- Select a cloud provider (AWS, Google Cloud, or Azure) and a region close to the location.
- Click Create Cluster and wait about 1–3 minutes.
- Under Database Access, create a username and password for the database.
- Under Network Access, add the IP address (0.0.0.0/0 allows access from anywhere for development).
- Click Connect on the cluster to get a connection string.
Atlas also includes a built-in web interface called Atlas Data Explorer where documents, collections, and databases are managed directly from the browser.
Option 2 — Installing MongoDB Locally
For those who prefer to work offline or on a local machine, MongoDB Community Edition is available as a free download.
Installing MongoDB on Windows
- Go to https://www.mongodb.com/try/download/community.
- Select Windows as the platform and choose the latest version.
- Download the .msi installer file.
- Run the installer and follow the setup wizard. Choose Complete installation.
- During installation, check the option to install MongoDB as a Service — this means MongoDB starts automatically when Windows starts.
- Also install MongoDB Compass (a graphical interface) if prompted — it is very helpful for beginners.
After installation, MongoDB starts as a background service. No manual starting is needed.
Installing MongoDB on macOS
The easiest way on macOS is through Homebrew, a popular package manager.
- Open the Terminal.
- Run this command to add the MongoDB repository:
brew tap mongodb/brew- Install MongoDB Community Edition:
brew install mongodb-community@7.0- Start the MongoDB service:
brew services start mongodb-community@7.0Installing MongoDB on Linux (Ubuntu)
- Import the MongoDB public key:
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -- Add the 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 the package list and install MongoDB:
sudo apt-get update
sudo apt-get install -y mongodb-org- Start the MongoDB service:
sudo systemctl start mongod- Enable MongoDB to start on system boot:
sudo systemctl enable mongodVerifying the Installation
Once MongoDB is installed, the installation can be verified by checking the version. Open the terminal or command prompt and type:
mongod --versionThe output shows the installed MongoDB version, confirming that the installation was successful.
Connecting to MongoDB Using the Shell
MongoDB comes with a command-line tool called the MongoDB Shell (mongosh). It allows direct interaction with the database by typing commands.
To open the shell, type:
mongoshOnce connected, the shell prompt changes to something like:
test>This means the shell is connected and ready. The word test is the default database that opens automatically.
Basic Shell Commands to Get Started
| Command | What It Does |
|---|---|
show dbs | Lists all existing databases |
use schoolDB | Switches to a database called schoolDB (creates it if it doesn't exist) |
show collections | Lists all collections in the current database |
db | Shows the name of the currently active database |
exit | Closes the MongoDB shell |
Example: Creating a Database
use schoolDBOutput:
switched to db schoolDBAt this point, the database schoolDB does not actually exist yet in MongoDB. It gets created automatically the moment the first document or collection is added to it.
MongoDB Compass — Graphical Interface
MongoDB Compass is a free desktop application with a visual interface for managing MongoDB databases. It is ideal for those who prefer working with menus and buttons rather than command-line text.
With Compass, databases, collections, and individual documents are visible in a clean layout. Queries can be built using a form-based interface, and results display in a readable format.
How to Connect Compass to Local MongoDB
- Open MongoDB Compass.
- In the connection field, enter:
mongodb://localhost:27017 - Click Connect.
The default port for MongoDB is 27017. This is where MongoDB listens for connections on the local machine.
Understanding the Default Port and Host
- Host:
localhost— This refers to the local machine. - Port:
27017— This is the default port MongoDB listens on.
When connecting any application or tool to MongoDB locally, these two values are used in the connection string unless they are changed during setup.
Summary
MongoDB can be set up either on the cloud using MongoDB Atlas or installed locally on Windows, macOS, or Linux. The MongoDB Shell (mongosh) provides a command-line interface to interact with the database. MongoDB Compass offers a visual alternative. The default connection runs on localhost port 27017. Creating a database with the use command makes it active, and it gets saved once data is inserted into it.
