MongoDB Getting Started
Getting started with MongoDB takes only a few minutes. You can either install MongoDB on your own computer or use MongoDB Atlas, a hosted cloud database with a free tier. Either way you will use mongosh, the MongoDB Shell, to type commands.
Option 1: MongoDB Atlas (Cloud)
- Sign up at mongodb.com/atlas and create a free M0 cluster
- Create a database user with a username and password
- Under Network Access, add your current IP address
- Click Connect → Shell and copy the connection string
Option 2: Install Locally
Download MongoDB Community Server for Windows, macOS or Linux and install mongosh. On macOS Homebrew works well; with Docker a single command starts a server.
# Docker
docker run -d --name mongo -p 27017:27017 mongo:7
# macOS (Homebrew)
brew tap mongodb/brew
brew install mongodb-community@7.0
brew services start mongodb-community@7.0Container started. MongoDB is listening on localhost:27017
Connecting with mongosh
Run mongosh with no arguments to connect to a local server, or pass an Atlas connection string.
mongosh
# or
mongosh "mongodb+srv://cluster0.abcde.mongodb.net/" --username naeemCurrent Mongosh Log ID: 6720a2... Connecting to: mongodb://127.0.0.1:27017/ Using MongoDB: 7.0.14 Using Mongosh: 2.3.1 test>
Useful First Commands
show dbs lists databases, use switches database, show collections lists collections and db.stats() gives sizes.
show dbs
use shop
db.customers.insertOne({ name: "Alpha Traders", city: "Lahore" })
show collectionsadmin 40.00 KiB
config 72.00 KiB
local 40.00 KiB
switched to db shop
{ acknowledged: true, insertedId: ObjectId('6720a3b1c2b9e81d4f3a1b02') }
customersGUI Tools
MongoDB Compass is the official free desktop app to browse collections, run queries and view index usage visually. The VS Code MongoDB extension lets you run playground scripts directly from your editor.
Frequently asked questions
What is the default MongoDB port?
MongoDB listens on port 27017 by default.
What is the difference between mongo and mongosh?
mongo was the legacy shell and was removed in MongoDB 6.0. mongosh is the modern replacement with syntax highlighting, autocomplete and better error messages.
Why can I not connect to my Atlas cluster?
The most common causes are that your IP address is not in the Network Access list, the password contains special characters that are not URL-encoded, or a firewall blocks port 27017.