Script Valley
MongoDB: Complete Course
MongoDB FoundationsLesson 1.2

How to install MongoDB and connect with mongosh

MongoDB Community Server installation, mongosh CLI, connection string URI, show dbs command, use command, Atlas free tier

Two ways to get started

MongoDB connection options

You can install MongoDB Community Server locally on your machine or use MongoDB Atlas, the fully managed cloud service. Atlas provides a permanent free tier cluster (512 MB) and eliminates setup time โ€” ideal for learning and early development. For production self-hosting, the local installation gives you full control over configuration and storage.

Local install on macOS

# Install with Homebrew
brew tap mongodb/brew
brew install mongodb-community@7.0
brew services start mongodb-community@7.0

# Connect with the mongosh shell
mongosh
# Output: Connecting to mongodb://127.0.0.1:27017

Atlas connection

# Paste your connection string from the Atlas dashboard
mongosh "mongodb+srv://cluster0.xxxxx.mongodb.net/" --username yourUser

Essential shell commands

show dbs           // list all databases with sizes
use shopDB         // switch active database context
show collections   // list collections in current db
db                 // print the current database name
db.stats()         // document counts and storage info

MongoDB uses lazy creation โ€” running use shopDB does not write anything to disk. The database only persists once you insert a document. Developers coming from SQL are often surprised that show dbs will not list an empty database they just created in the shell session.

One common pitfall: the mongosh shell session is stateful, but MongoDB itself is not. If your machine restarts before you insert data, the database context you set with use shopDB is gone and the database never existed. This is by design โ€” MongoDB only allocates storage when there is actual data to store, keeping the storage footprint minimal for development environments.

Up next

How MongoDB BSON and ObjectId work under the hood

Sign in to track progress

How to install MongoDB and connect with mongosh โ€” MongoDB Foundations โ€” MongoDB: Complete Course โ€” Script Valley โ€” Script Valley