Skip to main content

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.

  1. Download the MongoDB installer from the official MongoDB website
  2. Run the .msi installer and follow the setup wizard
  3. Choose "Complete" installation type
  4. Install MongoDB Compass (the GUI) when prompted - it's helpful for visual learners
  5. The installer will set up MongoDB as a Windows Service automatically
Verify installation in PowerShell
# Check if MongoDB service is running
Get-Service "MongoDB"

# Expected output: Status should be "Running"
tip

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

Manage MongoDB service in Windows
# Start MongoDB service
Start-Service "MongoDB"

# Stop MongoDB service
Stop-Service "MongoDB"

# Restart MongoDB service
Restart-Service "MongoDB"

macOS/Linux Service Management

Manage MongoDB service on Unix systems
# 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.

Connect to local MongoDB instance
// 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:

Basic database operations in mongosh
// 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()
note

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:

Comprehensive installation test
// 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
  1. 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
  2. When is a MongoDB database actually created?

    • A) When you run the use command
    • B) When you create the first collection
    • C) When you insert the first document
    • D) When you explicitly run db.createDatabase()
  3. What is the default port that MongoDB listens on?

    • A) 3306
    • B) 5432
    • C) 27017
    • D) 8080
  4. Which command shows all databases in your MongoDB instance?

    • A) db.showDatabases()
    • B) show databases
    • C) list dbs
    • D) show dbs
  5. 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:

  1. C) use
  2. C) When you insert the first document
  3. C) 27017
  4. D) show dbs
  5. C) It shows a connection error