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
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:27017Atlas connection
# Paste your connection string from the Atlas dashboard
mongosh "mongodb+srv://cluster0.xxxxx.mongodb.net/" --username yourUserEssential 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 infoMongoDB 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.
