MongoDB NoSQL
Step-by-Step Guide to Setting Up MongoDB on macOS
January 1, 2025
0

Setting up MongoDB on macOS can be a straightforward process if you follow the right steps. Here’s how I did it on my macOS ARM 64 system using MongoDB Community Server version 8.04.

Step 1: Download MongoDB

I started by downloading MongoDB from the MongoDB Community Server. I selected the following options:

  • Version: 8.04
  • Platform: macOS ARM 64
  • Package: tgz

Step 2: Extract and Set Up MongoDB

Once the download was complete, I extracted the contents of the tgz file. Then, I created a directory for MongoDB in /usr/local and copied the contents of the bin folder from the extracted files into the new directory:

sudo mkdir -p /usr/local/mongodb
sudo cp -R <extracted-folder>/bin /usr/local/mongodb

Step 3: Create a Data Directory

MongoDB requires a data directory to store its data. I created this directory in my home folder:

mkdir -p ~/data/db

Then, I ensured that I had the correct permissions for this directory:

sudo chown -R $(whoami) ~/data/db

Step 4: Start MongoDB Service

With everything in place, I started the MongoDB service by running the following command in the terminal:

/usr/local/mongodb/mongod --dbpath ~/data/db

This command starts the MongoDB server and points it to the data directory I created earlier.

Step 5: Install and Run Mongo Shell

Initially, when I tried to run the MongoDB shell using the mongosh command, it didn’t work because mongosh was not installed. To fix this, I used Homebrew to install the MongoDB shell:

brew install mongosh

After installation, I was able to start the MongoDB shell by simply typing:

mongosh

This opened the MongoDB terminal, allowing me to interact with my database.

Step 6: Use MongoDB Compass for a GUI

If you prefer a graphical user interface (GUI) to interact with your MongoDB databases, you can download MongoDB Compass. It provides an intuitive way to visualize, explore, and manage your data.

Step 7: MongoDB Basics

In MongoDB, what’s called a “table” in MySQL is referred to as a “collection.” Here are some basic commands I used to get started:

  • Show Databases:
    show dbs
  • Get current Default Database:
    db
  • Get All Collection of current DB:
    show collections

 

  • Switch/Create a Database:
    use myDatabase
  • Finding Data (by default it shows first 20 records):
    db.books.find()
  • Finding Next 20 records by just typing:
    it
  • Finding Data by values that has rating 10:
    db.books.find({rating: 10})
  • If you want to see only author columns only:
    db.books.find({rating: 10},{author: 1})
    
  • If you want to see only author, title columns only:
    db.books.find({rating: 10},{author: 1, title: 1})
  • Update a Document in a Collection:
    db.myCollection.updateOne({ name: "Alice" }, { $set: { age: 26 } })
  • Update a Document in a Collection:
    db.myCollection.updateOne({ name: "Alice" }, { $set: { age: 26 } })
    
  • Update a Document in a Collection:
    db.myCollection.updateOne({ name: "Alice" }, { $set: { age: 26 } })

Conclusion

Setting up MongoDB on macOS was a great learning experience. The process required a few manual steps, but it was rewarding to see everything come together. Now, I can explore MongoDB’s powerful features and dive deeper into database management!

About author

ZERIN

CEO & Founder (BdBooking.com - Online Hotel Booking System), CEO & Founder (TaskGum.com - Task Managment Software), CEO & Founder (InnKeyPro.com - Hotel ERP), Software Engineer & Solution Architect

There are 0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *