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 sessions and move around databases and collections.

Start mongosh locally

Open an interactive shell to a local deployment.

bashANYmongoshshellconnect
bash
mongosh

Starts the modern MongoDB shell and connects using default local settings when available.

Connect with a MongoDB URI

Connect directly with a full connection string.

bashANYmongoshuriconnect
bash
mongosh "mongodb://localhost:27017/appdb"

Useful for choosing the target database immediately.

Connect with authentication

Pass username, password, and auth database.

bashANYmongoshauthuri
bash
mongosh "mongodb://dbuser:secret@localhost:27017/appdb?authSource=admin"

`authSource=admin` is common when the user is defined in the admin database.

List databases

Show visible databases in the deployment.

javascriptANYmongodbdatabases
javascript
show dbs

A quick overview of the databases you can access.

Switch database

Change the current database context.

javascriptANYmongodbdatabasecontext
javascript
use appdb

Subsequent collection operations use the selected database.

List collections

Show collections in the current database.

javascriptANYmongodbcollections
javascript
show collections

Useful when exploring a schema or checking whether a collection already exists.

Get collection names programmatically

Return an array of collection names.

javascriptANYmongodbcollectionsmongosh
javascript
db.getCollectionNames()

Convenient when you want a value instead of formatted shell output.

Check database stats

Inspect size, object count, and storage metrics.

javascriptANYmongodbstatsstorage
javascript
db.stats()

Helpful for quick diagnostics and growth checks.

Collection bootstrap

Create collections and inspect a few starter records.

Create a collection

Create a collection explicitly.

javascriptANYmongodbcollectioncreate
javascript
db.createCollection("users")

MongoDB can also create collections implicitly on first insert, but explicit creation is useful for options and predictability.

Insert one sample document

Create a starter document.

javascriptANYmongodbinsertOneusers
javascript
db.users.insertOne({ name: "Ada", email: "ada@example.com", active: true })

A collection will be created automatically if it does not already exist.

Read a few documents

Preview documents from a collection.

javascriptANYmongodbfindlimit
javascript
db.users.find().limit(5)

Limit early while exploring to keep output manageable.

Pretty-print query results

Format documents for easier reading.

javascriptANYmongodbfindpretty
javascript
db.users.find({ active: true }).pretty()

Useful for ad hoc inspection in interactive sessions.

Drop a collection

Remove a collection and its data.

javascriptANYmongodbdropcollection
javascript
db.users.drop()

Use with care because the operation deletes all documents in the collection.

Recommended next

No recommendations yet.