Installing MongoDB

Before you can write a single query, you need a running MongoDB server and a way to talk to it. Installing MongoDB really means two things: getting the mongod database server running somewhere (your own machine, a Docker container, or a managed cloud cluster), and installing mongosh, the modern JavaScript-based shell you use to connect and run commands. This lesson covers every path — local installs on macOS, Windows, and Linux, running MongoDB in Docker, and using the fully-managed MongoDB Atlas cloud service — plus how to verify the install and connect for the first time.

Overview: What “Installing MongoDB” Actually Involves

A MongoDB deployment has a few distinct pieces, and it helps to know which one you’re installing at each step:

  • mongod — the database server process. It listens on a TCP port (27017 by default), manages the on-disk data files, and executes queries. This is the actual “database” in MongoDB.
  • mongosh — the MongoDB Shell, a Node.js-based interactive JavaScript REPL. It is a client: it connects to a mongod (or a cluster of them) over the network using the MongoDB wire protocol, and every command you type — db.users.find(), show dbs, etc. — is sent to the server as a request and the result comes back and gets printed. mongosh replaced the legacy mongo shell, which is deprecated and no longer bundled with current MongoDB versions.
  • MongoDB Database Tools — a separate set of command-line utilities (mongodump, mongorestore, mongoimport, mongoexport) for backup, restore, and data import/export. They install independently of the server.
  • MongoDB Compass — an optional GUI for browsing data and building queries visually. Not required for this course, but handy if you prefer a visual client.

You have two broad routes to a running server: install mongod yourself (on your laptop, a VM, or in a container), or let MongoDB run it for you with MongoDB Atlas, the official managed cloud service, which gives you a free-tier cluster with no local installation of the server at all — you’d still install mongosh locally to connect to it. Most learners start with a local install because it’s simplest to reason about while you’re learning the fundamentals, then move to Atlas once they want something durable and shareable.

Installation Methods

macOS (Homebrew)

The official MongoDB Homebrew tap installs both the Community Server and keeps it updated:

brew tap mongodb/brew
brew update
brew install mongodb-community@7.0
brew services start mongodb-community@7.0

brew services start registers mongod as a background service that also restarts automatically on reboot — you won’t need to launch it manually again.

Ubuntu / Debian (APT)

On Linux, MongoDB is installed from its own APT repository (Ubuntu’s default repos often carry an outdated version), then started and enabled as a systemd service:

curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
  sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
  sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl enable mongod

systemctl enable mongod is the Linux equivalent of Homebrew’s service registration: it makes mongod start automatically on boot.

Windows

On Windows, download the .msi installer from the official MongoDB download center and run it. The installer bundles the option to “Install MongoDB as a Service,” which is checked by default — leave it checked so mongod starts automatically with Windows and you don’t need a terminal open to keep it running. mongosh is offered as a separate optional component in the same installer; make sure that checkbox is also selected.

Docker (any OS)

If you already have Docker, this is the fastest way to get a disposable MongoDB instance without touching your host OS at all:

docker run --name mongodb -d -p 27017:27017 mongo:7.0

This pulls the official mongo:7.0 image, runs it detached (-d), and maps container port 27017 to your host’s port 27017, so mongosh on your host can connect to localhost:27017 exactly as if MongoDB were installed natively. You’ll still install mongosh separately on the host (it isn’t required inside the container).

MongoDB Atlas (no local server needed)

Atlas is MongoDB’s managed cloud service. You create a free account, spin up a free-tier (M0) cluster from the web console, add your current IP address to the cluster’s network access list, and create a database user with a password. Atlas hands you back a connection string that mongosh can use directly — no mongod to install or manage yourself. This route is worth using once you want a database that survives your laptop restarting or that you can share with teammates.

Syntax: Connecting with mongosh

Once a server is running somewhere, connecting is a single command: mongosh <connection-string>. The general form of a connection string is:

Part Meaning
mongodb:// or mongodb+srv:// Protocol. Plain mongodb:// is used for a direct host:port; mongodb+srv:// is used for Atlas and other DNS seed-list deployments — it resolves the actual replica set members from a DNS record.
<user>:<password>@ Optional credentials, if the deployment requires authentication (Atlas always does; a fresh local install usually doesn’t yet).
<cluster-url> or host:port Where the server lives — localhost:27017 for a local install, or an Atlas cluster hostname like cluster0.abcde.mongodb.net.
/<database> Optional default database to connect into. If omitted, mongosh defaults to a database called test.
?<options> Optional query-string parameters, e.g. ?retryWrites=true&w=majority, commonly appended by Atlas’s provided string.

With no arguments at all, mongosh assumes mongodb://localhost:27017 — the default for anything installed locally.

Examples

Example 1: Verify the install

mongod --version
mongosh --version

Output:

db version v7.0.14
Build Info: { ... }

2.1.5

If either command isn’t found, the corresponding install step above didn’t complete, or the binary isn’t on your PATH (common on Windows if you skipped the “add to PATH” installer option).

Example 2: Connect locally and run your first commands

mongosh
// Inside the mongosh prompt
show dbs
use learnmongo
show collections

Output:

admin      40.00 KiB
config     12.00 KiB
local      72.00 KiB

switched to db learnmongo

show dbs, use <db>, and show collections are mongosh-only shortcuts — they aren’t JavaScript, they’re convenience syntax the shell recognizes interactively. Notice learnmongo doesn’t appear in show dbs yet: MongoDB doesn’t actually create a database (or a collection) until you insert the first document into it.

Example 3: Insert and read a document

db.students.insertOne({
  name: "Ava Chen",
  course: "MongoDB Fundamentals",
  enrolledAt: new Date()
});

db.students.find({ name: "Ava Chen" });

Output:

{
  acknowledged: true,
  insertedId: ObjectId('64f1a2b3c4d5e6f7a8b9c0d1')
}

[
  {
    _id: ObjectId('64f1a2b3c4d5e6f7a8b9c0d1'),
    name: 'Ava Chen',
    course: 'MongoDB Fundamentals',
    enrolledAt: ISODate('2026-08-03T00:00:00.000Z')
  }
]

That single insertOne is what actually creates both the learnmongo database and the students collection — MongoDB creates them lazily, on first write, which is why they weren’t listed a moment ago.

Example 4: Connect to an Atlas cluster instead

mongosh "mongodb+srv://<user>:<password>@<cluster-url>/mydb"

Quote the connection string in your OS shell — it often contains characters like & that your terminal would otherwise try to interpret. Once connected, every command you run (find, insertOne, aggregation pipelines) works identically whether the server is on your laptop, in Docker, or on Atlas — the shell doesn’t care where mongod physically lives.

How It Works Step by Step

  1. Package install: your package manager (Homebrew, APT, the Windows MSI, or the mongo Docker image) places the mongod binary, default config file, and a data directory on disk, and registers a service definition (systemd unit, Homebrew service, or Windows service) so the OS knows how to start and stop it.
  2. Starting the service: the service manager launches mongod, which reads its config file, opens the configured data directory (WiredTiger, MongoDB’s default storage engine, initializes or opens its data files there), and binds to a TCP port — 27017 by default, localhost-only unless you explicitly configure otherwise.
  3. Connecting with mongosh: when you run mongosh <connection-string>, the shell parses the string, resolves the host (or, for mongodb+srv://, does a DNS lookup to discover the real replica set members), opens a TCP connection, and performs a handshake — including authentication, if credentials were supplied.
  4. Running a command: every mongosh command you type that touches the database (not the interactive shortcuts) is serialized into a BSON-encoded wire-protocol message, sent to mongod, executed there, and the BSON result is sent back and pretty-printed by the shell.

Common Mistakes

Mistake 1: mongosh can’t connect because mongod was never started

Installing the package does not start the server automatically on every platform, and after certain crashes or manual stops it stays down until restarted.

mongosh
// MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

Check that the service is actually running and start it if not:

brew services start mongodb-community@7.0
# or on Linux:
sudo systemctl start mongod
sudo systemctl status mongod

Mistake 2: using the old mongo shell or its syntax

Tutorials written before 2021 reference the mongo shell, which is deprecated and not installed by current MongoDB versions at all. Following an old guide that says to run mongo will simply fail with “command not found.” Always install and use mongosh, and stick to current method names like insertOne/updateMany rather than legacy ones like insert/update/save, which older guides sometimes still show.

Mistake 3: Atlas connection times out because the IP isn’t allow-listed

Atlas blocks all network access by default until you explicitly add an IP (or a CIDR range) to the cluster’s Network Access list. Skipping this step produces a connection timeout that looks like a credentials problem but isn’t:

mongosh "mongodb+srv://user:pass@cluster0.abcde.mongodb.net/mydb"
# MongoServerSelectionError: connection timed out

Fix it in the Atlas web console under Network Access by adding your current IP (or, for quick learning purposes only, 0.0.0.0/0 to allow any IP — never leave that open on anything beyond a disposable practice cluster).

Best Practices

  • Match the MongoDB version in your tutorials/course to what you install — this course targets 7.0+; mixing an ancient mongo-shell tutorial with a fresh 7.0 install causes confusing syntax mismatches.
  • Let the OS service manager (systemd, Homebrew services, Windows services) manage mongod instead of running it manually in a terminal you have to keep open.
  • Use Docker or Atlas when you want a clean, disposable environment for experimenting without touching your host machine’s filesystem.
  • Never expose a local development mongod to the public internet without authentication enabled — the default local install binds to localhost specifically to prevent this.
  • For Atlas, scope Network Access to your actual IP ranges rather than 0.0.0.0/0 as soon as you’re doing anything beyond throwaway practice.
  • Keep the MongoDB Database Tools (mongodump/mongorestore) installed alongside the server — you’ll want them the first time you need a backup.

Practice Exercises

  • Install MongoDB Community Server and mongosh using whichever method matches your OS, then run mongod --version and mongosh --version to confirm both succeeded.
  • Start mongosh with no connection string, run show dbs, then insert one document into a new collection of your choosing and run show dbs again — notice when your new database first appears in the list.
  • Create a free MongoDB Atlas cluster, add your IP to Network Access, and connect to it with mongosh using its mongodb+srv:// connection string. Expected result: the shell prompt changes to reflect the connected cluster/database, confirming a successful remote connection.

Summary

  • mongod is the database server process; mongosh is the JavaScript shell client that connects to it — installing MongoDB means getting both in place.
  • You can install locally on macOS (Homebrew), Windows (MSI installer), or Linux (APT/YUM repo), run it in Docker for a disposable instance, or skip local installation entirely with managed MongoDB Atlas.
  • Local installs default to localhost:27017 with no authentication; Atlas requires credentials and an IP allow-list entry before it will accept a connection.
  • show dbs, use <db>, and show collections are mongosh-only shortcuts, not JavaScript — real queries like insertOne/find are.
  • MongoDB creates a database and collection lazily on the first write, not at connection time.
  • Always use current mongosh and modern method names — the legacy mongo shell and methods like insert/update/save are deprecated.