ACES

How to Install MongoDB and MongoDB Compass on Windows, macOS, and Linux: A Complete Guide

Learn how to install MongoDB and MongoDB Compass on Windows, macOS, and Linux with our easy-to-follow guide. Perfect for Node.js developers looking to enhance their database skills.

user
Sandip Sapkota

Mon, Jul 8, 2024

4 min read

thumbnail

How to Install MongoDB and MongoDB Compass on Windows, macOS, and Linux

Introduction

MongoDB is a widely-used NoSQL database appreciated for its flexibility and scalability. MongoDB Compass is a GUI tool for MongoDB that provides a user-friendly interface for database management and visualization. In this guide, we’ll walk you through installing MongoDB and MongoDB Compass on Windows using a GUI method and on macOS and Linux using the command line. This guide is especially created to help you with your ongoing Node.js training. 🚀

Table of Contents

  1. Installing MongoDB on Windows (GUI)
  2. Installing MongoDB on macOS (CLI)
  3. Installing MongoDB on Linux (CLI)
  4. Installing MongoDB Compass
  5. Basic MongoDB Commands for Linux and macOS

Installing MongoDB on Windows (GUI)

  1. Download the Installer:

  2. Run the Installer:

    • Double-click the downloaded .msi file to launch the setup wizard.
    • Choose "Complete" setup type to install MongoDB with default options.
  3. Configure the Service:

    • During installation, ensure "Install MongoDB as a Service" is selected.
    • You can specify the data directory and log directory or leave them as default.
  4. Complete Installation:

    • Click "Next" and then "Install" to complete the installation process.
    • Once finished, click "Finish" to exit the setup wizard.
  5. Verify Installation:

    • Open Command Prompt and run:
      mongod --version
      
    • You should see the version of MongoDB installed.
  6. Create Data Directory:

    • MongoDB expects the data directory to be at C:\data\db. Create this directory:
      mkdir C:\data\db
      
  7. Start MongoDB:

    • MongoDB should start automatically as a service. You can also manually start it using:
      net start MongoDB
      

Installing MongoDB on macOS (CLI)

  1. Install Homebrew:

    • If Homebrew is not installed, you can install it by running:
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
  2. Install MongoDB:

    • Use Homebrew to install MongoDB by adding the MongoDB tap and installing:
      brew tap mongodb/brew
      brew install mongodb-community@6.0
      
  3. Start MongoDB:

    • Start the MongoDB service with:
      brew services start mongodb/brew/mongodb-community
      
  4. Verify Installation:

    • Check the installed version with:
      mongod --version
      

Installing MongoDB on Linux (CLI)

Ubuntu/Debian

  1. Import the Public Key:

    wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
    
  2. Create a List File:

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
    
  3. Update Package List:

    sudo apt-get update
    
  4. Install MongoDB:

    sudo apt-get install -y mongodb-org
    
  5. Start MongoDB:

    sudo systemctl start mongod
    
  6. Enable MongoDB to Start on Boot:

    sudo systemctl enable mongod
    

RHEL/CentOS

  1. Create a Repository File:

    echo "[mongodb-org-6.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/6.0/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc" | sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo
    
  2. Install MongoDB:

    sudo yum install -y mongodb-org
    
  3. Start MongoDB:

    sudo systemctl start mongod
    
  4. Enable MongoDB to Start on Boot:

    sudo systemctl enable mongod
    

Installing MongoDB Compass

  1. Download MongoDB Compass:

  2. Install on Windows:

    • Download the .exe file and run the installer.
    • Follow the setup wizard to install MongoDB Compass.
  3. Install on macOS:

    • Download the .dmg file.
    • Open the .dmg file and drag MongoDB Compass to your Applications folder.
  4. Install on Linux:

    • Download the .deb file for Debian-based distributions or the .rpm file for RHEL-based distributions.
    • Use the appropriate package manager to install it:
      • For .deb:
        sudo dpkg -i <path_to_deb_file>
        
      • For .rpm:
        sudo rpm -ivh <path_to_rpm_file>
        

Basic MongoDB Commands for Linux and macOS

Start MongoDB Server

mongod

Connect to MongoDB

mongo

Create a Database

use myDatabase

Create a Collection

db.createCollection("myCollection")

Insert a Document

db.myCollection.insertOne({ name: "Alice", age: 25 })

Find Documents

db.myCollection.find()

Update a Document

db.myCollection.updateOne({ name: "Alice" }, { $set: { age: 26 } })

Delete a Document

db.myCollection.deleteOne({ name: "Alice" })

List Databases

show dbs

List Collections

show collections

Drop a Collection

db.myCollection.drop()

Drop a Database

db.dropDatabase()

Conclusion

By following this guide, you should now have MongoDB and MongoDB Compass installed on your system using the GUI on Windows and CLI on macOS and Linux. You can start exploring the powerful features of MongoDB for your development projects. If you run into any issues or have any questions, feel free to contact us. Happy coding! 😄