MongoDB Getting Started
Installing MongoDB
MongoDB Community Edition is free and available for Windows, macOS, and Linux. The installation method varies by platform:
| Platform | Method | Command / Notes |
|---|---|---|
| Windows | MSI Installer | Download from mongodb.com, run installer, add to PATH |
| macOS | Homebrew | brew tap mongodb/brew && brew install mongodb-community |
| Ubuntu/Debian | apt package manager | Import GPG key, add repo, apt install mongodb-org |
| RHEL/CentOS | yum package manager | Add repo file, yum install mongodb-org |
| Any platform | Docker | docker run -d -p 27017:27017 mongo:latest |
// Ubuntu 22.04 installation
// 1. Import MongoDB public GPG key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
// 2. Add the repository
echo "deb [ arch=amd64,arm64 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
// 3. Install MongoDB
sudo apt-get update
sudo apt-get install -y mongodb-org
// 4. Start the mongod service
sudo systemctl start mongod
sudo systemctl enable mongod // start on boot
// 5. Check status
sudo systemctl status mongod
// macOS with Homebrew
brew services start mongodb/brew/mongodb-community
Connecting with mongosh
The MongoDB Shell (mongosh) is the official interactive JavaScript interface for MongoDB. It replaces the legacy mongo shell and provides a modern REPL environment.
// Connect to local MongoDB (default port 27017)
mongosh
// Connect to a specific host and port
mongosh "mongodb://localhost:27017"
// Connect with authentication
mongosh "mongodb://username:password@localhost:27017/mydb"
// Connect to MongoDB Atlas
mongosh "mongodb+srv://username:password@cluster0.abc123.mongodb.net/mydb"
// --- Basic mongosh commands ---
// Show all databases
show dbs
// Switch to (or create) a database
use myapp
// Show current database
db
// Show all collections in current database
show collections
// Get database statistics
db.stats()
// Get collection statistics
db.users.stats()
// Get MongoDB server version
db.version()
// Exit the shell
exit
Your First Collection
use myapp
// Insert a document (creates the collection automatically)
db.users.insertOne({ name: "Alice", age: 29, city: "New York" })
// Read all documents
db.users.find()
// Pretty-print results
db.users.find().pretty()
// Insert more documents
db.users.insertMany([
{ name: "Bob", age: 34, city: "London" },
{ name: "Carol", age: 27, city: "Tokyo" }
])
// Count documents
db.users.countDocuments()
// Find with a filter
db.users.find({ city: "London" })
MongoDB Compass GUI
MongoDB Compass is the official graphical interface for MongoDB. It lets you visually explore your data, run queries, build aggregation pipelines, manage indexes, and analyze schema — all without writing shell commands. Download it free from mongodb.com/products/compass.
To connect Compass to a local instance, use the connection string: mongodb://localhost:27017
MongoDB Atlas Free Tier
MongoDB Atlas is the fully managed cloud database service. The free tier (M0 cluster) gives you 512MB of storage with no credit card required — perfect for learning and small projects. Sign up at cloud.mongodb.com, create a free cluster, and get a connection string to use with mongosh or any driver.