How can I list all collections in the MongoDB shell?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Listing collections is one of the first checks you do when exploring or debugging a MongoDB database. The shell provides both human-friendly and script-friendly options for this task. A robust workflow should also include database selection, permission checks, and awareness of differences between mongo and mongosh.
Connect and Select the Target Database
Start by opening the shell and connecting to the server instance.
Then select the database.
If the database does not exist yet, switching with use still succeeds, but collection listing may be empty until data is created.
List Collections with Shell Shortcut
The quickest interactive command is:
This prints collection names line by line, which is convenient for manual exploration.
If nothing appears, verify that:
- you are in the expected database
- your account has read privileges
- the database actually has user collections
Use API Method for Scriptable Output
For automation, prefer API-based output.
This returns an array and is easier to process in scripts.
Example with filtering:
Filtering out system collections can make audit output cleaner.
Richer Metadata with listCollections
If you need details such as collection type or options, use listCollections command.
This returns metadata useful for diagnostics and migration tooling.
You can also query through cursor API:
This includes information beyond name list and helps when validating special collection configurations.
Listing Collections from Command Line Without Interactive Session
For non-interactive jobs, run evaluation directly:
This is useful in CI checks and operational scripts.
If authentication is enabled:
Avoid embedding raw passwords in shell history for production environments. Prefer secure secret injection mechanisms.
Permissions and Role Requirements
Collection visibility depends on roles. Read-only users may see only permitted namespaces.
If expected collections are missing:
- run
db.getUser()for current user context where allowed - verify assigned roles on target database
- test with known admin account for comparison
This separates permission issues from data issues quickly.
Notes on Legacy mongo Shell
Older environments may still use mongo shell command. Core commands are similar, but modern tooling and scripting ergonomics are stronger in mongosh.
If maintaining mixed environments, document which shell version your scripts target to avoid compatibility surprises.
Multi-Database Inspection Workflow
When auditing an instance, list databases first, then iterate collection lists per database with explicit context switching. This avoids confusion caused by similarly named collections across environments. In administrative scripts, include current database name in output so exported reports remain unambiguous when reviewed later.
For production operations, store shell command outputs with timestamps and environment labels to make later incident comparisons straightforward.
Review access rules regularly.
Common Pitfalls
- Running collection listing on wrong database after forgetting to switch context.
- Assuming
usemeans database exists with data when it may still be empty. - Using
show collectionsin scripts where structured JSON output is needed. - Overlooking permission limits and interpreting missing collections as data loss.
- Hardcoding credentials in command history or shared scripts.
Summary
- Use
show collectionsfor quick interactive checks. - Use
db.getCollectionNames()orlistCollectionsfor scriptable and metadata-rich workflows. - Always confirm database context before interpreting output.
- Handle authentication and roles explicitly in operational scripts.
- Prefer
mongoshfor modern MongoDB shell automation and maintainability.

