MongoDB Shell and Connection Basics

Connect with mongosh, switch databases, inspect collections, and run the most common shell workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Connect and navigate
Start mongosh locally
mongosh

# Open an interactive shell to a local deployment.

Connect with a MongoDB URI
mongosh "mongodb://localhost:27017/appdb"

# Connect directly with a full connection string.

Connect with authentication
mongosh "mongodb://dbuser:secret@localhost:27017/appdb?authSource=admin"

# Pass username, password, and auth database.

List databases
show dbs

# Show visible databases in the deployment.

Switch database
use appdb

# Change the current database context.

List collections
show collections

# Show collections in the current database.

Get collection names programmatically
db.getCollectionNames()

# Return an array of collection names.

Check database stats
db.stats()

# Inspect size, object count, and storage metrics.

## Collection bootstrap
Create a collection
db.createCollection("users")

# Create a collection explicitly.

Insert one sample document
db.users.insertOne({ name: "Ada", email: "ada@example.com", active: true })

# Create a starter document.

Read a few documents
db.users.find().limit(5)

# Preview documents from a collection.

Pretty-print query results
db.users.find({ active: true }).pretty()

# Format documents for easier reading.

Drop a collection
db.users.drop()

# Remove a collection and its data.

Recommended next

No recommendations yet.