Yandex

MongoDB - Getting StartedMongoDB - Getting Started1

Installing MongoDB on Mac



Installing MongoDB on Mac

In this tutorial, you will learn how to install MongoDB on macOS using Homebrew, a popular package manager for Mac. By the end of this lesson, you'll have MongoDB running on your machine and ready to accept connections.

What is Homebrew?

Homebrew is a command-line tool that helps you install software packages easily on macOS. It’s like an App Store for your terminal.

Step 1: Check if Homebrew is Installed

First, open your Terminal and type:

    brew --version
    Homebrew 4.1.10

If you don’t have Homebrew installed, use the following command to install it:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Q: Why use Homebrew instead of downloading MongoDB manually?

A: Homebrew handles dependencies, updates, and setup automatically. It's quicker and more reliable than doing everything manually.

Step 2: Tap the MongoDB Homebrew Formula

MongoDB’s official tap (package source) must be added before installation:

    brew tap mongodb/brew

Explanation: This tells Homebrew to use MongoDB’s official source for the latest formula (install recipe).

Step 3: Install MongoDB Community Edition

Now install MongoDB with:

    brew install mongodb-community@7.0
    🍺  mongodb-community@7.0 was successfully installed!

Note: Replace 7.0 with the latest stable version if needed.

Step 4: Start the MongoDB Service

You can start the MongoDB server using the following command:

    brew services start mongodb/brew/mongodb-community
    Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

Q: What does this command do?

A: It tells macOS to run MongoDB as a background service, so you don’t have to start it manually every time.

Step 5: Verify MongoDB is Running

Use the following command to open the MongoDB shell:

    mongosh
    Current Mongosh Log ID: 661ffd8e4e2ff7d1723fcd65
    Connecting to: mongodb://127.0.0.1:27017
    test>

Explanation: You're now connected to the test database by default. You can start running MongoDB commands!

Step 6: Insert Your First Document

Let’s run a simple insert command to confirm everything is working:


    db.users.insertOne({ name: "Alice", age: 25 });
    {
      acknowledged: true,
      insertedId: ObjectId("...")
    }

Explanation: You just created a document inside a collection named users. MongoDB automatically creates the collection if it doesn't exist.

Stopping MongoDB (Optional)

To stop the MongoDB service:

    brew services stop mongodb/brew/mongodb-community
    Stopping `mongodb-community`... (might take a while)
    ==> Successfully stopped `mongodb-community` (label: homebrew.mxcl.mongodb-community)

Summary

  • You installed MongoDB using Homebrew
  • You started MongoDB as a background service
  • You verified installation using the shell
  • You inserted a sample document into a collection

You're now ready to build MongoDB-based applications on your Mac! Next, we’ll explore MongoDB Compass — a GUI to visualize and work with your database easily.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M