The Wayback Machine - https://web.archive.org/web/20240926144139/https://www.geeksforgeeks.org/how-to-install-mongodb-on-macos/
Open In App

How to install MongoDB on MacOS

Last Updated : 17 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

MongoDB is an open-source document-oriented database. It is categorized under the NoSQL(Not only SQL) database because the storage and retrieval of data in the MongoDB are not in the form of tables. This is the general introduction to MongoDB and now we learn how to install MongoDB on MacOS.

You can install MongoDB using two different methods:

  • Using .tgz tarball
  • Using Homebrew.

Here, we will learn how to install MongoDB using .tgz tarball. Before learning the installation process, please verify the system requirements mentioned below.

Requirements to Install MongoDB on MacOS

  • macOS 11 (Big Sur) or later is recommended.
  • A 64-bit processor (x86_64 architecture) is required.
  • The account running Mongod has read and write permissions for data and log directories.

Steps to Install MongoDB on MacOS Using .tgz tarball

To install MongoDB on MacOS, first download the MongoDB, and then setup the command line. Check the steps below, that explain the strep-by-step installation process for MongoDB.

Download MongoDB

Step 1: Go to MongoDB Download Center to download MongoDB Community Server. 
 

download mongodb

Here, You can select any version, OS, and package according to your requirement. For macOS we need to choose: 
 

  • Version: 4.2.2
  • OS: macOS x64
  • Package: TGZ

Step 2: When the download completes, you will get a TGZ file( with extension .tgz). Now, extract this file to wherever the location you want in your system. Like, as shown in the below image.: 
 

extract .tgz file

Here, the bin folder is the most important folder which contains all the MongoDB related executables like mongo for command-line interface to interact with the database, mongod is a database, mongodumps is used for creating a binary export of the contents of a database, etc.  

Setting Command Line Interface (mongo Shell)

Step 3: Now to run mongo shell we need to set an environment variable or path. You can set your path in .bash_profile file If you are using Mojave OS or below, or you can set your path in .zprofile file if you are using Catalina or above. If you do not have .bash_profile or .zprofile, then you can create them using the following two ways:  

  1. Simply open Text Editor and save a file with a name .bash_profile or .zprofile in your home directory. This file is hidden in your home directory if you wish to see them then press command + shift + . in finder(in the Home).
  2. You can also create them in the terminal by using the following commands:  
  • Go to home directory: cd ~/
  • Create a new file: touch .bash_profile or touch .zprofile
  • Opening these files for editing in TextEdit: open -e .bash_profile or open -e .zprofile
  • Read updated file: source ~/.bash_profile or source ~/.zprofile 

After creating or getting .bash_profile or .zprofile set your path. Like, as shown in the below image: 

seetting path

Here, “/Users/anki/mongodb-macos-x86_64-4.2.2/bin” is the path where the .tgz file is extracted and “:$PATH” allows to run the paths(other than MongoDB) presents in the .bash_profile or .zprofile. After entering the path in the .bash_profile or .zprofile save that file and then the terminal read the updated path using the source command. 
 

terminal

Important Point: For Catalina users when you run mongo or mongo –nodb command in your terminal, your macOS would not allow to run mongo. So, to run mongo you need to give them permission manually i.e,

System Preferences > Security & Privacy > Allow Anyway

Same procedure to run mongod. 

After setting up the command-line interface (mongo Shell) of MongoDB when you try to start mongo Shell, it will look like as shown in the below image, here the connections are failing because we do not have a database server(i.e, mongod). To run mongo Shell you need a MongoDB server, i.e, mongod which will run locally on our system. 
 

error while trying to run mongo shelll

Setting Database Server (mongod)

Step 4: Before running mongod first we will create a folder or directory which will store all the data processed by the mongod. Or in other words, we will create a data directory in which mongod process will write data. 

Important Point: For those who are using macOS Mojave or below they are allowed to use MongoDB default data directory of /data/db. But those who are using macOS Catalina or above are not allowed to use MongoDB default data directory of /data/db because Apple restricted access so, you need to create a different data directory and you are allowed to keep that directory wherever you want in your system. Or in other words, Catalina users are not able to use /data/db as a database folder instead of this you can use a non-root path like ~/data/db. Like, as shown in the below image. 
 

Here, MongoDB is the data directory where the mongod writes the data. 

Step 5: Now we are going to run mongod with its data directory path. Here, mongod is the database server provided by the MongoDB. To start mongod you can use one of the following commands:  

 mongod --dbpath /data/db
Or
sudo mongod --dbpath /data/db

Here, sudo command means allow to give all the permissions to start the database. Like, as shown in the below image:  

sudo command

Here, sudo mongod –dbpath ~/MongoDB command to start mongod server and ~/MongoDB is data directory.  

Run mongo Shell

Step 6: Now we are going to connect our server (mongod) with mongo shell. So, keep that mongod terminal window and open a new terminal window and write mongo and now our mongo shell will successfully connect to the mongod. 

Important Point: Please do not close the mongod terminal window if you close this terminal window your server will stop working and you will not connect to the mongo shell. 
 

connecti mongod and mongo shell

Now, you are ready to write queries in the mongo Shell. 

Also Read



Previous Article
Next Article

Similar Reads

Connect MongoDB (AWS) from Local Machine using WinSCP and MongoDB Compass
Pre-requisite: AWS and MongoDB In this article, we are going to connect to the Mongo database of the AWS Ubuntu Server instance using WinSCP and learn how to get connected to the server from your local machine using MongoDB Compass.  If you haven't installed a MongoDB server in your AWS EC2 instance then follow the steps present in this article and
4 min read
MongoDB - Delete Multiple Documents Using MongoDB Shell
The db.collection.deleteMany() method is used to delete multiple documents from a collection in Mongo Shell. This method deletes multiple documents from the collection according to the filter. The deleteMany() is a Mongo shell method, which can delete multiple documents. This method can be used in multi-document transactions. If you use this method
2 min read
MongoDB Compass vs MongoDB Atlas
MongoDB is a popular NoSQL database known for flexibility and scalability. MongoDB Compass offers a graphical interface for managing databases visually while MongoDB Atlas is a cloud-based service for automated management and scaling of MongoDB databases. In this article, We will learn about the in-depth understanding of the difference between the
5 min read
How to Install MongoDB to WAMP?
Everyone has heard about Mongo DB. MongoDB is an open-source tool that supports NoSQL. It is document-oriented not object-oriented. MongoDB is usually used when large data is set to manage information. Here we discuss the installation of MongoDB on Wamp. Before we know about what is Mongo DB and its features. Let's discuss this shortly. Mongo DB Mo
2 min read
How to Install and Setup MongoDB Plugin on IntelliJ?
MongoDB is an open-source platform for the usage of databases. It is the software that allows playing with databases. Like MySQL, MongoDB is also a famous software in this field. MongoDB is document-oriented and a no SQL database. It helps you to make an interactive database. For installing MongoDB as a plugin on IntelliJ, the user needs first to i
4 min read
How to Install MongoDB on CentOS?
MongoDB is a flexible, powerful, and super-fast database management system. Unlike those old-school databases with strict rules, MongoDB lets you store your data in flexible documents, similar to how you organize things in JSON. This means you can easily add new information or change things up without much hassle. In this article, we will install M
4 min read
How to Install and Configure MongoDB in Ubuntu?
MongoDB is a popular NoSQL database offering flexibility, scalability, and ease of use. Installing and configuring MongoDB in Ubuntu is a straightforward process, but it requires careful attention to detail to ensure a smooth setup. In this guide, we'll learn how to install and configure MongoDB in Ubuntu. We'll walk you through each step, from ins
6 min read
How to Install MongoDB Enterprise on Windows
MongoDB is a free, open-source, cross-platform, document-oriented database. The MongoDB Enterprise Edition is a commercial version of MongoDB used for enterprise-scale operations. In this guide, we will learn how to install MongoDB Enterprise edition on a Windows System. We have provided a detailed step-by-step guide with images for installing Mong
2 min read
Install MongoDB Compass on Windows
MongoDB Compass is a powerful graphical user interface for MongoDB which is designed to simplify database management. It offers features like easy querying, index visualization, an aggregation pipeline builder and schema and structure analysis. In this article, we will learn How to Install MongoDB Compass on Windows in detail and so on. Features of
3 min read
How to Install MongoDB on Windows?
Installing MongoDB on Windows involves setting up a 64-bit version of the database management system, which supports platforms like Windows Server 2022, Windows Server 2019 and Windows 11. The process includes downloading the MongoDB server, configuring the environment and running the MongoDB service. In this article, we will learn How to Install M
6 min read
What is macOS?
macOS is built on a Unix based architecture and provides users with a reliable and user friendly experience. In this article, we will understand macOS in detail. What is macOS?macOS operating system that Apple Inc. developed especially for its Macintosh computer series. It is the replacement for the classic MacOS. The reliability, security features
4 min read
MongoDB vs MySQL
Differences between MongoDB and MySQL on various parameters are discussed below: What are MongoDB and MySQL? MongoDB MySQL MongoDB is an open-source database developed by MongoDB, Inc. MongoDB stores data in JSON-like documents that can vary in structure. It is a popular NoSQL database. MySQL is a popular open-source relational database management
2 min read
Difference between Oracle and MongoDB
1. Oracle : Oracle is a relational database management system (RDBMS). It was developed by Oracle Corporation in 1980. It is the first database designed for grid computing that provides the most flexible and cost-effective way to manage information and application. It runs on major platforms like Windows, Unix, Linux, and macOS. It is a relational
2 min read
Introduction about Node.js and MongoDB
NoSQL databases are databases that store and retrieve the data that is present in a non-tabular format. Depending on the format of the data stored, NoSQL databases are split into 4 major types: Key-Value Graph database Document-oriented Column family These databases are built to address the shortcomings of traditional RDBMS and provide high perform
4 min read
MongoDB | Delete Database using MongoShell
Prerequisite : MongoDB Introduction Short Description : A MongoDB Database is a container for all the collections, where Collection is a bunch of MongoDB documents similar to tables in RDBMS and Document is made of fields similar to a tuple in RDBMS, but it has a dynamic schema here. Example of a Document : { "Name" : "Aman", Age : 24, Gender : "Ma
2 min read
Difference between RDBMS and MongoDB
MongoDB is an open-source document-oriented database used for high volume data storage. It falls under the classification of a NoSQL database. NoSQL tool means that it doesn't utilize the usual rows and columns. MongoDB uses BSON (document storage format) which is a binary style of JSON documents. Features of MongoDB: Multiple Servers: It can run o
2 min read
How MongoDB works ?
MongoDB is an open-source document-oriented database. It is used to store a larger amount of data and also allows you to work with that data. MongoDB is not based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data, that's why known as NoSQL database. Here, the term ‘NoSQL
3 min read
MongoDB - Embedded Documents
MongoDB provides you a cool feature which is known as Embedded or Nested Document. Embedded document or nested documents are those types of documents which contain a document inside another document. Or in other words, when a collection has a document, this document contains another document, another document contains another sub-document, and so o
2 min read
MongoDB - Insert Single Document Using MongoShell
In MongoDB, insert operations are used to add new documents in the collection. If the collection does not exist, then the insert operations create the collection by inserting documents. Or if the collection exists, then insert operations and add new documents to the existing collection. You are allowed to add a single document in the collection usi
2 min read
MongoDB - Insert Multiple Document Using MongoShell
In MongoDB, insert operations are used to add new documents in the collection. If the collection does not exist, then the insert operations create the collection by inserting documents. Or if the collection exists, then insert operations add new documents in the existing collection. You are allowed to insert multiple documents in the collection by
2 min read
MongoDB - Update Single Document Using MongoShell
In MongoDB, you can update a single document in the collection using db.collection.updateOne() method. This method updates a document in the collection according to the filter. updateOne() is a mongo shell method, which can updates one document at a time. This method update the first document that satisfies the given condition in the collection. Or
4 min read
MongoDB - Update Multiple Documents Using MongoShell
In MongoDB, you can update multiple documents in the collection using db.collection.updateMany() method. This method updates all the documents in the collection that match the given filter. updateMany() is a mongo shell method, which can update multiple document. This method only accepts the documents that contain update operator expressions. updat
3 min read
MongoDB - Replace Documents Using MongoShell
In MongoDB, you are allowed to replace an existing document with a new document in the collection with the help of db.collection.replaceOne() method. This method will replace the existing document with the replacement document. replaceOne() is a mongo shell method, which only replaces one document at a time. The replacement document may contain dif
4 min read
MongoDB - Query Documents using Mongo Shell
MongoDB provides you read operations to retrieve documents from the collection or query a collection for a document. You can perform read operations using the db.collection.find() method. This method selects or views the documents from the collection and returns the cursor to the selected document. find() is a mongo shell method, which can be used
4 min read
MongoDB Projection
MongoDB provides a special feature that is known as Projection. It allows you to select only the necessary data rather than selecting whole data from the document. For example, a document contains 5 fields, i.e., { name: "Roma", age: 30, branch: EEE, department: "HR", salary: 20000 } But we only want to display the name and the age of the employee
2 min read
MongoDB - Query Embedded Documents Using Mongo Shell
MongoDB provides you read operations to retrieve embedded/nested documents from the collection or query a collection for a embedded/nested document. You can perform read operations using the db.collection.find() method. This method selects or views embedded/nested documents of the collection and returns the cursor to the selected document. Syntax:
3 min read
MongoDB AND operator ( $and )
MongoDB provides different types of logical query operators and $and operator is one of them. This operator is used to perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array. You can use this operator in methods like find(), update(), etc. ac
2 min read
MongoDB NOT operator ( $not )
MongoDB provides different types of logical query operators and $not operator is one of them. This operator is used to perform logical NOT operation on the specified operator expressions and select or retrieve only those documents that do not match the given operator expression. It also includes those documents that don't contain the field. You can
2 min read
MongoDB OR operator ( $or )
MongoDB provides different types of logical query operators and $or operator is one of them. This operator is used to perform logical OR operation on the array of two or more expressions and select or retrieve only those documents that match at least one of the given expression in the array. You can use this operator in methods like find(), update(
2 min read
MongoDB NOR operator ( $nor )
MongoDB provides different types of logical query operators and $nor operator is one of them. This operator is used to perform logical NOR operation on the array of one or more expressions and select or retrieve only those documents that do not match all the given expression in the array. You can use this operator in methods like find(), update(),
2 min read
Article Tags :