Installing MongoDB and Setting Up Your Environment
Now that you understand MongoDB's role in the NoSQL landscape, let's get your hands dirty by setting up a working MongoDB environment. In this lesson, you'll install MongoDB, verify the installation, and learn basic database operations.
Learning Goals:
- Install MongoDB Community Edition on your operating system
- Start and stop the MongoDB server
- Connect to MongoDB using the MongoDB Shell
- Create your first database and collection
- Understand basic MongoDB service management
Installation Methods
MongoDB offers several installation options depending on your needs and environment. We'll focus on the Community Edition, which is free and perfect for learning and development.
Option 1: MongoDB Community Server
The most common approach is installing MongoDB Community Server directly on your machine.
- Windows
- macOS
- Linux
- Download the MongoDB installer from the official MongoDB website
- Run the
.msiinstaller and follow the setup wizard - Choose "Complete" installation type
- Install MongoDB Compass (the GUI) when prompted - it's helpful for visual learners
- The installer will set up MongoDB as a Windows Service automatically
# Check if MongoDB service is running
Get-Service "MongoDB"
# Expected output: Status should be "Running"
Using Homebrew is the simplest method on macOS:
# Tap the MongoDB formula
brew tap mongodb/brew
# Install MongoDB Community Edition
brew install mongodb-community
# Start MongoDB service
brew services start mongodb-community
On Ubuntu, use the official MongoDB repository:
# Import the public key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
# Create list file
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
# Update and install
sudo apt-get update
sudo apt-get install -y mongodb-org
# Start MongoDB
sudo systemctl start mongod
If you prefer not to install MongoDB locally, consider using MongoDB Atlas (the cloud version). We'll cover this in detail in Lesson 13, but it's a great zero-installation option for getting started quickly.
Starting MongoDB Server
Once installed, you need to start the MongoDB server process. The method varies by operating system.
Windows Service Management
# Start MongoDB service
Start-Service "MongoDB"
# Stop MongoDB service
Stop-Service "MongoDB"
# Restart MongoDB service
Restart-Service "MongoDB"
macOS/Linux Service Management
# Start MongoDB
sudo systemctl start mongod
# Stop MongoDB
sudo systemctl stop mongod
# Check status
sudo systemctl status mongod
# Enable automatic startup on boot
sudo systemctl enable mongod
Connecting with MongoDB Shell
The MongoDB Shell (mongosh) is your primary tool for interacting with MongoDB databases. It's a powerful JavaScript interface that comes bundled with MongoDB installation.
// Simply run mongosh in your terminal
mongosh
// You should see connection information and a prompt like:
// test>
Your First Database Operations
Let's create your first database and perform basic operations:
// Switch to (or create) a database called "school"
use school
// Insert your first document into a "students" collection
db.students.insertOne({
name: "Alice Johnson",
age: 20,
major: "Computer Science",
enrolled: true
})
// Find all documents in the students collection
db.students.find()
// Count documents in the collection
db.students.countDocuments()
In MongoDB, databases and collections are created automatically when you first insert data into them. The use school command doesn't actually create the database until you insert your first document.
Verifying Your Installation
Let's run a comprehensive test to ensure everything is working correctly:
// Check server status and version
db.version()
// Check current database
db.getName()
// List all databases
show dbs
// List collections in current database
show collections
// Test a complete workflow
use test_database
db.employees.insertOne({ name: "Test User", department: "IT" })
db.employees.find()
db.employees.drop()
Common Pitfalls
- Port conflicts: MongoDB uses port 27017 by default. Ensure no other applications are using this port
- Data directory permissions: On Linux/macOS, MongoDB needs write permissions to
/data/db(default data directory) - Service not starting: Check system logs for error messages - common issues include insufficient disk space or memory
- mongosh command not found: Ensure MongoDB binaries are in your system PATH
- Authentication errors: By default, MongoDB runs without authentication for local development
Summary
You've successfully installed MongoDB and set up your development environment! You can now:
- Install MongoDB on your preferred operating system
- Start and stop the MongoDB server
- Connect to MongoDB using the MongoDB Shell
- Create databases and collections
- Perform basic document insertion and querying
Your environment is now ready for the real fun - working with MongoDB data types and document structures in the next lesson!
Show quiz
-
What command do you use to switch to (or create) a database in MongoDB Shell?
- A)
create database - B)
switch database - C)
use - D)
connect
- A)
-
When is a MongoDB database actually created?
- A) When you run the
usecommand - B) When you create the first collection
- C) When you insert the first document
- D) When you explicitly run
db.createDatabase()
- A) When you run the
-
What is the default port that MongoDB listens on?
- A) 3306
- B) 5432
- C) 27017
- D) 8080
-
Which command shows all databases in your MongoDB instance?
- A)
db.showDatabases() - B)
show databases - C)
list dbs - D)
show dbs
- A)
-
What happens if you try to use MongoDB Shell without the server running?
- A) It starts the server automatically
- B) It connects to a cloud instance instead
- C) It shows a connection error
- D) It works in offline mode
Answers:
- C)
use - C) When you insert the first document
- C) 27017
- D)
show dbs - C) It shows a connection error