Course Outline (Part 3)

Now that you understand what MongoDB is, it’s time to get our hands dirty. Before we can start writing queries and building applications, we need to set up our database environment.

There are two primary ways to use MongoDB:

  1. Local Installation: Running the database directly on your own computer (great for learning and offline development).
  2. MongoDB Atlas: Using a fully managed cloud database provided by MongoDB (great for production and modern web apps).

In this module, we will cover how to set up both, along with a powerful graphical tool called MongoDB Compass.


System Requirements

Before installing MongoDB Community Edition locally, ensure your system meets the basic requirements. Because it is highly optimized, it can run on surprisingly modest hardware for development purposes:

  • RAM: Minimum 2GB (4GB+ recommended)
  • Storage: At least 10GB of free space (SSD highly recommended for database performance)
  • OS: Windows 10/11, macOS 11+, or modern Linux distributions (Ubuntu 20.04+, Debian, RHEL)

Install MongoDB on Windows

Installing MongoDB on Windows is straightforward using the official MSI installer.

  1. Download the Installer:
    • Go to the official MongoDB Download Center.
    • Under the “MongoDB Community Server” tab, select Windows as your platform and msi as the package. Click Download.
  2. Run the Installer:
    • Open the .msi file you just downloaded.
    • Click Next and accept the terms of the license agreement.
    • Choose the Complete setup type.
  3. Service Configuration:
    • On the Service Configuration screen, leave “Install MongoDB as a Service” checked. This ensures the database runs automatically in the background whenever you start your computer.
    • Leave the default service name and data directories as they are.
  4. Install MongoDB Compass:
    • On the next screen, leave the checkbox for “Install MongoDB Compass” checked. We will use this tool later.
    • Click Install and wait for the process to finish.

Install MongoDB on macOS

The easiest and most officially recommended way to install MongoDB on macOS is using Homebrew, the popular Mac package manager.

  1. Install Homebrew (if you don’t have it already): Open your Terminal and paste the following command:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Download the Official MongoDB Tap:
    brew tap mongodb/brew
  3. Install MongoDB Community Server:
    brew install mongodb-community@7.0
    (Note: 7.0 is the major version at the time of writing. You can omit the @7.0 to always get the latest version).
  4. Start the MongoDB Service: To start the database and have it run in the background, type:
    brew services start mongodb-community@7.0

Install MongoDB on Ubuntu/Linux

Installing on Linux involves importing the public GPG key and using the APT package manager. We will use Ubuntu as our example.

  1. Import the public key: Open your terminal and run:
    curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
       sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
       --dearmor
  2. Create a list file for MongoDB:
    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
  3. Reload the local package database:
    sudo apt-get update
  4. Install the MongoDB packages:
    sudo apt-get install -y mongodb-org
  5. Start MongoDB:
    sudo systemctl start mongod
    sudo systemctl enable mongod # To ensure it starts on boot

Install MongoDB Using Docker

If you prefer keeping your computer clean and are familiar with containers, Docker is an amazing way to run MongoDB locally.

  1. Make sure Docker Desktop is installed and running.
  2. Open your terminal and run the following command to pull the image and start a container:
    docker run --name my-mongo -p 27017:27017 -d mongo
    • --name my-mongo gives the container a recognizable name.
    • -p 27017:27017 maps the default MongoDB port from the container to your local machine.
    • -d runs the container in the background (detached mode).

Verify Installation

Regardless of which operating system you used, we need to make sure the database is actually running.

By default, MongoDB runs on port 27017. We can test if it is listening by attempting to connect to it using the MongoDB Shell (mongosh), which is installed alongside the server.

Open your terminal or command prompt and type:

mongosh

If the installation was successful, your terminal will connect to the database. You will see some connection information and your terminal prompt will change to:

test>

You can type exit to leave the shell for now.


MongoDB Atlas Setup (Cloud Database)

While a local installation is great, building modern web apps usually requires your database to live in the cloud. MongoDB Atlas is the official database-as-a-service from MongoDB Inc. It offers a generous forever-free tier that is perfect for learning and small projects.

Here is how to set up your free cloud database:

  1. Create an Account: Go to mongodb.com/atlas and sign up for a free account.
  2. Build a Database:
    • Once logged in, click Build a Database.
    • Select the M0 FREE shared cluster option.
    • Choose a Cloud Provider (AWS, Google Cloud, or Azure) and select a region closest to you.
    • Click Create.
  3. Security Setup:
    • Create a Database User: You will be prompted to create a username and password. Save this password! You will need it to connect later.
    • Network Access: You must whitelist IP addresses that are allowed to connect to your database. For local development, click “Add My Current IP Address”. (If your IP changes often, you can add 0.0.0.0/0 to allow access from anywhere, though this is less secure).
  4. Get Your Connection String:
    • Once the cluster finishes deploying (it takes a minute or two), click the Connect button on your cluster dashboard.
    • Choose “Compass” or “Drivers” to reveal your unique connection string (it looks like a URL starting with mongodb+srv://).

MongoDB Compass Installation

While developers love the command line, looking at raw JSON data in a black terminal window gets exhausting.

MongoDB Compass is the official Graphical User Interface (GUI) for MongoDB. It allows you to explore your data visually, run queries with a few clicks, and visually build complex data pipelines.

If you didn’t install it during the Windows setup:

  1. Go to the Compass Download Page.
  2. Download the version for your operating system.
  3. Install and open the application.

How to Connect using Compass:

  1. Open MongoDB Compass.
  2. You will see a large input field for a “Connection String”.
    • For Local Databases: Type mongodb://localhost:27017 and click Connect.
    • For MongoDB Atlas: Paste the mongodb+srv://... connection string you copied from the Atlas dashboard. Make sure to replace <password> in the string with the actual password you created!
  3. Click Connect.

You should now see a visual dashboard showing the default administrative databases (admin, config, local).


Summary

Congratulations! You have successfully configured your database environment. Whether you chose a local installation, Docker, or the cloud-based MongoDB Atlas, you now have a powerful NoSQL engine ready to store your data.

In Module 4, we will dive into the command line and master the MongoDB Shell (mongosh) to execute our very first commands!

Discussion

Loading comments...