⬅ Previous Topic
Installing MongoDB on LinuxNext Topic ⮕
Introduction to MongoDB Atlas (Cloud)⬅ Previous Topic
Installing MongoDB on LinuxNext Topic ⮕
Introduction to MongoDB Atlas (Cloud)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.
Homebrew is a command-line tool that helps you install software packages easily on macOS. It’s like an App Store for your terminal.
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.
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).
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.
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.
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!
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.
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)
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.
⬅ Previous Topic
Installing MongoDB on LinuxNext Topic ⮕
Introduction to MongoDB Atlas (Cloud)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.