mongosh (MongoDB Shell) is the official interactive command-line interface for MongoDB. Think of it as your direct conversation with the database โ you type commands, it responds with data.
| SQL World | MongoDB World |
|---|---|
mysql CLI | mongosh |
sqlcmd (SQL Server) | mongosh |
psql (PostgreSQL) | mongosh |
If MongoDB is running locally on the default port, just type:
mongosh
This is shorthand for:
mongosh "mongodb://localhost:27017"
mongodb:// localhost : 27017 / myDatabase
โโโโโโโโ โโโโโโโโโ โโโโโ โโโโโโโโโโ
protocol host port database (optional)
// Connects and switches to "myShop" immediately mongosh "mongodb://localhost:27017/myShop"
// When your server requires a username/password mongosh "mongodb://localhost:27017" --username [user] --password [pass] // Or in the connection string mongosh "mongodb://[user]:[pass]@localhost:27017/admin"
test> or myShop> โ this shows which database you're currently using.
These are the commands you'll use constantly. Here's the SQL equivalent for each:
test> show dbs // Output: // admin 40.00 KiB // config 108.00 KiB // local 40.00 KiB
| MongoDB | SQL Equivalent |
|---|---|
show dbs | SHOW DATABASES; (MySQL) or query sys.databases (SQL Server) |
show dbs until it has at least one document in it. Creating a database with use alone isn't enough โ it's "lazy created."
test> use myShop // Output: switched to db myShop myShop>
If the database doesn't exist yet, MongoDB doesn't complain โ it just creates it when you first insert data. No CREATE DATABASE needed.
| MongoDB | SQL Equivalent |
|---|---|
use myShop | USE myShop; (MySQL) or USE [myShop]; (SQL Server) |
myShop> db // Output: myShop
| MongoDB | SQL Equivalent |
|---|---|
db | SELECT DATABASE(); (MySQL) or SELECT DB_NAME(); (SQL Server) |
myShop> show collections // Output: // products // orders // customers
| MongoDB | SQL Equivalent |
|---|---|
show collections | SHOW TABLES; (MySQL) or query sys.tables (SQL Server) |
You'll learn these in depth later, but here's a taste so you can experiment in the shell right now:
// Switch to a practice database
use practice
// Insert a document (creates the collection automatically)
db.users.insertOne({
name: "Alex",
age: 28,
role: "developer"
})
// Find all documents in the collection
db.users.find()
// Find with a filter (like WHERE)
db.users.find({ role: "developer" })
// Count documents
db.users.countDocuments()
// Drop the collection when done experimenting
db.users.drop()
// Drop the entire database
db.dropDatabase()
use a database and insertOne() into a collection โ both are created automatically on first write. This is very different from SQL where you must CREATE DATABASE and CREATE TABLE first.
Type db. and press Tab โ mongosh will show you all available methods. Works for collection names too.
Press โ and โ arrow keys to cycle through previous commands, just like any terminal.
mongosh automatically detects incomplete expressions. If you open a { and press Enter, it waits for the closing }:
myShop> db.users.insertOne({
... name: "Sam",
... age: 32
... })
// General help help // Database-level help db.help() // Collection-level help db.users.help()
For longer commands, enter editor mode:
myShop> .editor
// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)
db.products.insertMany([
{ name: "Laptop", price: 999, category: "electronics" },
{ name: "Mouse", price: 29, category: "electronics" },
{ name: "Desk", price: 250, category: "furniture" }
])
// Press Ctrl+D to execute
cls // or console.clear()
exit // or quit // or press Ctrl+C twice
| Command | What It Does | SQL Equivalent |
|---|---|---|
mongosh | Connect to local MongoDB | Open SQL CLI |
show dbs | List all databases | SHOW DATABASES |
use myDb | Switch to / create database | USE myDb / CREATE DATABASE |
db | Show current database name | SELECT DATABASE() |
show collections | List collections in current db | SHOW TABLES |
db.col.find() | Show all documents | SELECT * FROM col |
db.col.insertOne({...}) | Insert one document | INSERT INTO col ... |
db.col.countDocuments() | Count documents | SELECT COUNT(*) FROM col |
db.col.drop() | Delete a collection | DROP TABLE col |
db.dropDatabase() | Delete current database | DROP DATABASE |
Open mongosh and complete these exercises:
playground and insert this document into a notes collection:
{ title: "My first note", content: "Learning mongosh!", createdAt: new Date() }
Exercise 3: Run show dbs โ does playground appear now?db.notes.find() to see your document. Notice the _id field that MongoDB added automatically.playground database with db.dropDatabase().
1. What command lists all databases in MongoDB?
2. What happens when you run use newDb and newDb doesn't exist?
3. When does a new database first appear in show dbs?
4. What language does mongosh use under the hood?