✍️

MongoDB Shell Commands Explained – show dbs, use, db, help & More

MongoDB Shell Basics for Beginners | Essential MongoDB Commands Guide

MongoDB Shell Basics for Beginners

Learn essential MongoDB commands like show dbs, use database_name, show collections, db, and help with simple examples.


What is MongoDB Shell?

MongoDB Shell, also called mongosh, is a command-line tool used to interact with MongoDB databases. It allows developers to create databases, switch databases, view collections, insert data, fetch data, and manage MongoDB using commands.

If you are learning backend development, MERN stack, or database management, MongoDB Shell basics are very important.

In this guide, you will learn:
  • show dbs
  • use database_name
  • show collections
  • db
  • help

How to Open MongoDB Shell

After installing MongoDB, open Command Prompt and run the following command:

mongosh

If MongoDB Shell opens successfully, you may see something like this:

test>

This means MongoDB Shell is ready to use.

1. show dbs Command in MongoDB

The show dbs command is used to display all databases available in MongoDB.

Syntax

show dbs

Example Output

admin      40.00 KiB
config    108.00 KiB
local      72.00 KiB
testdb     40.00 KiB

Explanation

Database Purpose
admin Administrative database
config MongoDB configuration database
local Local server database
testdb User-created database
Note: MongoDB only shows databases that contain data. If a database is empty, it may not appear in the list.

2. use database_name Command in MongoDB

The use database_name command is used to switch to a specific database. If the database does not exist, MongoDB creates it automatically when you insert data.

Syntax

use database_name

Example

use school

Output

switched to db school

Now you are working inside the school database.

Automatic Database Creation Example

MongoDB creates the database automatically when you insert data:

db.students.insertOne({
  name: "Harsh",
  age: 21
})

This command creates the students collection inside the selected database if it does not already exist.

3. show collections Command in MongoDB

The show collections command displays all collections available inside the current database.

Syntax

show collections

Example Output

students
teachers
courses

These are collections inside the current database.

What is a Collection?

A collection in MongoDB is like a table in SQL. It stores related documents.

SQL MongoDB
Table Collection
Row Document
Column Field
Example:
Database: school
Collection: students
Document: { name: "Harsh", age: 21 }

4. db Command in MongoDB

The db command shows the name of the current active database.

Syntax

db

Example Output

school

This means you are currently using the school database.

Tip: If you are working with multiple databases, use the db command to check your current database.

5. help Command in MongoDB

The help command displays helpful MongoDB Shell commands and guidance.

Syntax

help

Example Help Commands

show dbs
show collections
use <db_name>
db.help()
db.collection.help()

If you forget any command, type help in MongoDB Shell.

Complete MongoDB Shell Example

Here is a simple example using all basic MongoDB Shell commands:

show dbs

use college

db

show collections

help

MongoDB Shell Workflow for Beginners

A beginner usually follows this workflow while using MongoDB Shell:

mongosh

show dbs

use testdb

db.users.insertOne({
  name: "Harsh",
  age: 21
})

show collections

db.users.find()

This workflow helps you open MongoDB Shell, select a database, insert data, view collections, and fetch documents.

Common Beginner Mistakes in MongoDB Shell

1. Database Not Showing in show dbs

If your database is not showing in show dbs, it means no data has been inserted yet.

Fix:

db.test.insertOne({
  name: "demo"
})

2. show collections Returns Empty

This happens when no collection exists inside the current database.

Insert data into a collection first, then run show collections.

3. Forgetting Current Database

Use this command anytime:

db

It shows the current active database.

Difference Between SQL and MongoDB Commands

SQL Command MongoDB Command
SHOW DATABASES show dbs
USE database use database_name
SHOW TABLES show collections
SELECT DATABASE() db

Why MongoDB Shell is Important for Developers

MongoDB Shell is important because it gives developers direct control over databases. It is useful for backend development, MERN stack projects, API development, testing queries, and database management.

  • Helps manage MongoDB databases quickly
  • Improves backend development skills
  • Makes debugging easier
  • Useful for learning CRUD operations
  • Important for MERN stack developers

Final Thoughts

MongoDB Shell basics are essential for every beginner. Commands like show dbs, use database_name, show collections, db, and help are used regularly while working with MongoDB.

Once you understand these commands, you can move toward advanced MongoDB topics like CRUD operations, queries, aggregation pipeline, Mongoose, Express.js integration, and MERN stack development.

Conclusion: Learning MongoDB Shell commands is the first step toward becoming confident in database management and backend development.

Official MongoDB Resources

❮ Previous Next ❯