Installation and mongosh

Introduction

Before you insert a document, you need a running MongoDB server and the mongosh shell to talk to it. This chapter covers Docker (recommended for development), native installs on Linux and macOS, Windows via WSL, and your first commands—use, show dbs, and ping. By the end, local MongoDB answers on port 27017.

Prerequisites

  • A terminal (Linux, macOS, or Windows with WSL2)
  • What Is MongoDB — terminology
  • Docker Desktop optional but recommended

Docker gives a consistent MongoDB 7 instance without touching system packages.

bash
# Pull the official image
docker pull mongo:7
 
# Run with a named volume so data survives container restarts
docker run -d \
  --name mongodb \
  -p 27017:27017 \
  -v mongo_data:/data/db \
  mongo:7

Verify the container:

bash
docker ps
docker logs mongodb --tail 20

Connect with mongosh inside the container:

bash
docker exec -it mongodb mongosh

Or install mongosh on your host and connect to localhost:

bash
mongosh "mongodb://127.0.0.1:27017"

Tip

Persist Data

The mongo_data volume keeps databases across docker rm. To wipe dev data: docker volume rm mongo_data (destructive).

Docker Compose (Optional)

docker-compose.yml:

yaml
services:
  mongodb:
    image: mongo:7
    ports:
      - "27017:27017"
    volumes:
      - mongo_data:/data/db
 
volumes:
  mongo_data:
bash
docker compose up -d

Linux (Ubuntu / Debian)

Official MongoDB packages:

bash
# Import MongoDB public GPG key and add repo (see mongodb.com/docs for your distro version)
# Simplified: many teams use Docker instead for dev parity
 
sudo apt update
sudo apt install -y mongodb-mongosh
 
# If mongod is installed via package manager:
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod

Ping from mongosh:

bash
mongosh "mongodb://127.0.0.1:27017" --eval 'db.runCommand({ ping: 1 })'

Expected: { ok: 1 }.

macOS (Homebrew)

bash
brew tap mongodb/brew
brew install mongodb-community@7.0
brew install mongosh
 
# Start as a service
brew services start mongodb-community@7.0
 
# Or run in foreground for debugging
mongod --config /opt/homebrew/etc/mongod.conf

Connect:

bash
mongosh

Windows

Native mongod on Windows is supported via MSI installer from MongoDB.com. For consistency with Linux deploys, many teams use WSL2 + Docker:

powershell
wsl --install

Inside WSL Ubuntu, follow the Docker steps above.

Install mongosh on Windows from the MongoDB download center if you need a host-side shell.

MongoDB Atlas (Cloud Preview)

For cloud-hosted dev:

  1. Create a free M0 cluster at mongodb.com/atlas
  2. Add your IP to Network Access
  3. Create a database user
  4. Copy the connection string: mongodb+srv://user:pass@cluster.mongodb.net/
bash
mongosh "mongodb+srv://USER:PASSWORD@cluster0.example.mongodb.net/mydb"

Full Atlas workflow — MongoDB Atlas.

mongosh Basics

Launch interactive shell:

bash
mongosh
# or with explicit URI
mongosh "mongodb://127.0.0.1:27017"

Essential commands

Code explanation:

  • use mydb switches context; the database file is created when you first store data
  • show dbs size reflects data files on disk, not empty switched databases

Run one-liners from the terminal

bash
mongosh --quiet --eval 'db.runCommand({ ping: 1 })'
mongosh hello_demo --eval 'db.stats()'

Run a script file

seed.js:

javascript
// seed.js — run with: mongosh hello_demo seed.js
db.items.insertOne({ name: "First item", createdAt: new Date() })
print("Seeded one item")
bash
mongosh hello_demo seed.js

Connection URI Format

text
mongodb://[username:password@]host1[:port1][,...]/[database][?options]
mongodb+srv://user:pass@cluster.example.mongodb.net/mydb?retryWrites=true&w=majority
PartExample
Host127.0.0.1:27017
Authadmin:secret@
Database/hello_demo
Options?authSource=admin

URL-encode special characters in passwords.

GUI: MongoDB Compass

MongoDB Compass provides visual browsing, schema sampling, and index management. Connect with the same URI as mongosh. Compass complements—not replaces—the shell for learning.

Common Problems

SymptomFix
Connection refusedStart mongod or Docker container; check port 27017
mongosh: command not foundInstall mongosh package separately on some distros
Docker port in useStop other MongoDB or map -p 27018:27017
Atlas timeoutIP whitelist, wrong password, firewall
Empty show dbs after useNormal until first insert

Post-Chapter Checklist

  • MongoDB runs locally (Docker or native)
  • mongosh connects and ping returns ok: 1
  • You can use a database and show collections
  • You know default port 27017 and URI format

FAQ

mongo vs mongosh?

Legacy mongo shell is removed in MongoDB 6+. Use mongosh only.

Do I need auth for local dev?

Single-node local dev often runs without auth. Enable users before exposing to a network—Security.

How much RAM does MongoDB need?

Dev: 512 MB–1 GB container limit is fine. Production sizing depends on working set—Performance.

Can I run MongoDB and MySQL together?

Yes—common pattern: MySQL for billing, MongoDB for content. Different ports (27017 vs 3306).

Upgrade MongoDB version?

Read release notes; backup with mongodump before major upgrades.