Getting Started with MongoDB: Installation and Setup Guide
MongoDB is a database that’s great for handling large amounts of diverse data. This article walks you through installing MongoDB and using the MongoDB Shell to manage your data easily.
Image by Editor (Kanwal Mehreen)| Canva
MongoDB is a popular NoSQL database. Unlike traditional databases, it stores data in documents instead of tables. These documents are in a format called BSON, similar to JSON. MongoDB is flexible and can handle large amounts of unstructured data.
It is widely used in modern applications that need to manage data quickly and scale easily. MongoDB can store a variety of data types and is designed to be fast and scalable. It is used in fields like web development, data analysis, and mobile applications.
Setting up MongoDB is simple, and once it is installed, you can use it to create and manage databases and collections. The MongoDB Shell (mongosh) lets you interact with the database using simple commands.
System Requirements
Before proceeding with the installation, make sure that your system meets the following requirements:
Minimum System Requirements
- Operating System: Linux, macOS, or Windows (MongoDB supports all major platforms).
- RAM: At least 4 GB of RAM (8 GB or more is recommended for larger datasets).
- Disk Space: At least 2 GB of free space for the base installation; more space will be needed for data storage.
Recommended System Requirements
- CPU: 64-bit architecture with multiple cores for optimal performance.
- RAM: 8 GB or more for larger or more demanding applications.
- Disk: SSD storage for faster read and write operations.
Installing MongoDB Community Edition
MongoDB Community Edition is free to use and perfect for learning and development. You can easily install MongoDB on Windows, macOS, or Linux.
Installing MongoDB on Windows
- Download MongoDB: Visit the MongoDB Download Center and select the appropriate version for your system.
- Run the Installer: Choose "Complete" setup when prompted and follow the installation steps.
- Add MongoDB to System PATH: After installation, add the MongoDB bin directory to your PATH to access it from the command line.
- Create Data and Log Directories: MongoDB needs a data directory to store its files. Open a command prompt and create the necessary directories.
- Start MongoDB: To start the MongoDB server, open a terminal and run the following command.
mkdir C:\data\db
mkdir C:\data\log
mongod --dbpath="C:\data\db"
Installing MongoDB on macOS
- Install Homebrew: Homebrew is a package manager for macOS that simplifies software installation. Open Terminal and install Homebrew
- Install MongoDB Community Edition: Run the following commands.
- Start MongoDB: To start MongoDB as a background service, run the following command.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew tap mongodb/brew
brew install mongodb-community
brew services start mongodb/brew/mongodb-community
Installing MongoDB on Linux (Ubuntu)
- Add the MongoDB Repository: Run the following commands.
- Install MongoDB: Update your package list and install MongoDB.
- Start MongoDB: Start the MongoDB service by running the following command.
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /etc/apt/trusted.gpg.d/mongodb.gpg --dearmor
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
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl start mongod
Installing the MongoDB Shell
Starting with MongoDB 5.0, the MongoDB Shell (mongosh) is a separate tool. It’s better than the old mongo shell. It has improved syntax highlighting and error messages. It also works well with modern JavaScript.
- Download mongosh: Go to the MongoDB Shell Download Page and select the version for your operating system.
- Install mongosh: Follow the installation instructions specific to your OS. On Windows, run the installer; on macOS and Linux, you can extract and move it to your PATH.
- Verify Installation: Open a terminal and run the following command.
mongosh
Basic Commands in the MongoDB Shell
Once you're connected, the MongoDB Shell lets you perform several tasks with your database.
Show Databases
show dbs
Select a Database
use
Switches to a specified database, creating it if it doesn't already exist.
Show Collections
show collections
Lists all collections (tables) within the current database.
CRUD Operations in MongoDB Shell
Insert Documents
db.collection.insertOne({ key: value })
db.collection.insertMany([{ key1: value1 }, { key2: value2 }])
Inserts one or multiple documents into the specified collection.
Find Documents
db.collection.find({ key: value })
Fetches documents matching the query. You can also use findOne() to get a single document.
Update Documents
db.collection.updateOne({ key: value }, { $set: { keyToUpdate: newValue } })
db.collection.updateMany({ key: value }, { $set: { keyToUpdate: newValue } })
Updates one or multiple documents matching the filter.
Delete Documents
db.collection.deleteOne({ key: value })
db.collection.deleteMany({ key: value })
Deletes one or multiple documents matching the filter.
Database Administration
Drop a Collection
db.collection.drop()
Removes the specified collection from the database.
Drop a Database
db.dropDatabase()
Deletes the current database.
Create an Index
db.collection.createIndex({ key: 1 })
Creates an index on the specified field(s) to improve query performance.
Additional Commands
Count Documents
db.collection.countDocuments({ key: value })
Counts documents that match a query.
Explain Query Plan
db.collection.find({ key: value }).explain("executionStats")
Shows the query plan and performance details, which can be useful for optimizing queries.
Backup Database
Use mongodump command in the terminal:
mongodump --db --out
Conclusion
MongoDB is a NoSQL database that stores data in JSON-like documents. It is easy to install on Windows, macOS, and Linux. The MongoDB Shell (mongosh) helps you work with the database. You can use it to find, add, change, and delete data. MongoDB also has commands to manage databases, collections, and indexes.
Jayita Gulati is a machine learning enthusiast and technical writer driven by her passion for building machine learning models. She holds a Master's degree in Computer Science from the University of Liverpool.