Installing MongoDB & mongosh
⚙️ Installing MongoDB & mongosh
Installing MongoDB Community Server
MongoDB Community Server is free and installs on Windows, macOS, and Linux — on Debian/Ubuntu, it's available via apt once MongoDB's own package repository is added, the same general package-manager pattern already familiar from the Linux course's software-installation chapter (linux_lesson_10). If you've already been through the Docker course, running MongoDB in a container is an even quicker way to get started without a native install at all:
Running MongoDB in Docker
This pulls the official MongoDB image and exposes it on its default port, 27017 — the same port a native install listens on.
mongosh: The MongoDB Shell
mongosh is MongoDB's modern interactive shell (the successor to an older, now-legacy mongo shell) — a JavaScript-based command line for running commands directly against a MongoDB instance. Launching it with no arguments connects to a MongoDB instance running on localhost, port 27017, by default:
Launching mongosh and Running a First Command
test is the default database mongosh starts you in — nothing special has to exist yet for the shell itself to connect.
Your First Commands
| Command | What It Does |
|---|---|
show dbs | List all databases that currently have data |
use <name> | Switch to (or prepare to create) a database |
show collections | List collections in the current database |
db.stats() | Show storage statistics for the current database |
MongoDB Compass: The GUI
Compass is MongoDB's official graphical client — browse databases, collections, and documents visually, build queries with a form-based interface, and inspect indexes, all without typing shell commands. It's entirely optional: everything Compass does, mongosh can also do — Compass is a convenience layer for visual exploration, not a separate capability.
mongosh vs. Compass
mongosh (CLI)
Scriptable, fast for repeated commands, works well over SSH to a remote server, the natural fit once you're writing real application code later in this course.
Compass (GUI)
Visual browsing of documents and schemas, easier for exploring unfamiliar data, handy for building and testing a query before writing it into code.
Connecting from a Connection String
Anything that connects to MongoDB — mongosh, Compass, or a driver in application code — ultimately uses a connection string in the form mongodb://<host>:<port>/<database>:
A Local Connection String
(MongoDB Atlas, the managed cloud version, uses a mongodb+srv:// variant instead — not covered in depth here, since this course focuses on a self-hosted instance, but worth knowing the prefix differs if you ever connect to a cloud-hosted cluster.) Chapter 8 uses this exact connection-string format to connect from real Node.js code.
💻 Coding Challenges
Challenge 1: Create and Confirm a Database
Write the mongosh commands to switch to a new database called shop, then confirm what you'd need to do for it to actually appear in show dbs.
Goal: Practice the basic mongosh workflow, including this chapter's tip-box gotcha about when a database actually gets created.
Challenge 2: Write a Connection String
Write the connection string for a MongoDB instance running on host db.internal, port 27018, targeting a database named inventory.
Goal: Practice the connection-string format that every MongoDB client ultimately relies on.
Challenge 3: mongosh or Compass?
Recommend mongosh or Compass for (a) exploring an unfamiliar database's document shapes for the first time, and (b) writing a deploy script that seeds some initial data automatically. Justify each.
Goal: Practice matching the right tool to the task, not defaulting to one for everything.
Running use shop in mongosh switches your session's context to a database called shop — but if shop doesn't exist yet, MongoDB doesn't actually create it at that moment. Run show dbs right after, and shop won't appear in the list at all. MongoDB only actually creates the database (and persists it) once you insert at least one document into a collection within it. Until then, use shop is really just telling the shell "point future commands at this name," not "create this database now" — a subtle difference from a MySQL CREATE DATABASE statement, which takes effect immediately.
🎯 What's Next
With a working instance and shell, the next chapter starts writing real data: CRUD Basics: Inserting and Finding Documents — insertOne/insertMany, find/findOne, and the query operators used to filter results.