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

  1. Go to https://www.mongodb.com/cloud/atlas and click Try Free.
  2. Sign up with an email address or a Google account.
  3. Choose the Free Tier (M0) cluster — this gives a free shared cluster with 512 MB of storage.
  4. Select a cloud provider (AWS, Google Cloud, or Azure) and a region close to the location.
  5. Click Create Cluster and wait about 1–3 minutes.
  6. Under Database Access, create a username and password for the database.
  7. Under Network Access, add the IP address (0.0.0.0/0 allows access from anywhere for development).
  8. 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

  1. Go to https://www.mongodb.com/try/download/community.
  2. Select Windows as the platform and choose the latest version.
  3. Download the .msi installer file.
  4. Run the installer and follow the setup wizard. Choose Complete installation.
  5. During installation, check the option to install MongoDB as a Service — this means MongoDB starts automatically when Windows starts.
  6. 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.

  1. Open the Terminal.
  2. Run this command to add the MongoDB repository:
brew tap mongodb/brew
  1. Install MongoDB Community Edition:
brew install mongodb-community@7.0
  1. Start the MongoDB service:
brew services start mongodb-community@7.0

Installing MongoDB on Linux (Ubuntu)

  1. Import the MongoDB public key:
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
  1. 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
  1. Update the package list and install MongoDB:
sudo apt-get update
sudo apt-get install -y mongodb-org
  1. Start the MongoDB service:
sudo systemctl start mongod
  1. Enable MongoDB to start on system boot:
sudo systemctl enable mongod

Verifying the Installation

Once MongoDB is installed, the installation can be verified by checking the version. Open the terminal or command prompt and type:

mongod --version

The 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:

mongosh

Once 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

CommandWhat It Does
show dbsLists all existing databases
use schoolDBSwitches to a database called schoolDB (creates it if it doesn't exist)
show collectionsLists all collections in the current database
dbShows the name of the currently active database
exitCloses the MongoDB shell

Example: Creating a Database

use schoolDB

Output:

switched to db schoolDB

At 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

  1. Open MongoDB Compass.
  2. In the connection field, enter: mongodb://localhost:27017
  3. 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.

Leave a Comment