MongoDB
Database Management
Coding
Technology
MongoDB Shell

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.

bash
mongosh "mongodb://localhost:27017"

Then select the database.

javascript
use sampleDB

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:

javascript
show collections

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.

javascript
db.getCollectionNames()

This returns an array and is easier to process in scripts.

Example with filtering:

javascript
db.getCollectionNames().filter(name => !name.startsWith("system."))

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.

javascript
db.runCommand({ listCollections: 1, nameOnly: false })

This returns metadata useful for diagnostics and migration tooling.

You can also query through cursor API:

javascript
db.getCollectionInfos()

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:

bash
mongosh "mongodb://localhost:27017/sampleDB" --quiet --eval "db.getCollectionNames()"

This is useful in CI checks and operational scripts.

If authentication is enabled:

bash
mongosh "mongodb://user:pass@localhost:27017/sampleDB?authSource=admin" --quiet --eval "db.getCollectionNames()"

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:

  1. run db.getUser() for current user context where allowed
  2. verify assigned roles on target database
  3. 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.

javascript
1db.adminCommand({ listDatabases: 1 }).databases.forEach(d => {
2  print("Database:", d.name);
3  const database = db.getSiblingDB(d.name);
4  printjson(database.getCollectionNames());
5});

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 use means database exists with data when it may still be empty.
  • Using show collections in 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 collections for quick interactive checks.
  • Use db.getCollectionNames() or listCollections for scriptable and metadata-rich workflows.
  • Always confirm database context before interpreting output.
  • Handle authentication and roles explicitly in operational scripts.
  • Prefer mongosh for modern MongoDB shell automation and maintainability.

Course illustration
Course illustration

All Rights Reserved.