MongoDB Databases

Beginner
6 min read
Updated: Nov 2026
Getting Started

What Is Databases?

A MongoDB database is a container for collections. One MongoDB server can host many databases, each with its own collections and its own files on disk. In mongosh you switch databases with use, and the current one is always available as the db variable. A database is created lazily: it only really exists after you store data in it.

Why It Matters

ReasonBenefit
Logical separationKeep each application or environment (dev, test) in its own database on one server.
Access controlRoles can be granted per database, so a service user sees only its own data.
Simple cleanupDropping a database removes a whole test dataset in one command.
Lazy creationYou do not need a separate create step, which keeps scripts and demos short.

How To Use It

  1. Connect with mongosh and run db to see the current database (usually test).
  2. Run show dbs to list databases that already contain data.
  3. Switch with use blog; mongosh replies that it switched to db blog.
  4. Insert one document, for example db.posts.insertOne({ title: "Hello" }).
  5. Run show dbs again and notice blog now appears.
  6. Check size and counts with db.stats().
  7. Remove it with db.dropDatabase() when you are done experimenting.

mongosh Examples

mongosh
// Which database am I in?
db

// Switch (lazily creates "blog")
use blog

// Nothing is saved yet, so blog is not listed
show dbs

// First write makes the database real
db.posts.insertOne({ title: "Hello MongoDB", views: 0 })

show dbs

db.getName()
db.stats()

// Danger: removes the database you are currently using
db.dropDatabase()

Edge Cases & Pitfalls

Empty databases do not show up

If you use a database but never write to it, show dbs will not list it. This is normal, not a bug.

dropDatabase affects the current database

Always run db to confirm where you are before calling db.dropDatabase(). There is no undo.

Naming rules

Names are case-sensitive, cannot be empty, and cannot contain characters such as /, \, ., a space, quotes, $, or the null character.

Do not store app data in admin, local, or config

Those databases are reserved for the server. Create your own database for application data.

Cheat Sheet

TopicDetail
use dbNameSwitch the current database; creates it lazily on first write
dbPrints the name of the current database
show dbsList databases that contain data (also: show databases)
db.getName()Return the current database name as a string
db.stats()Show size and object counts for the current database
db.dropDatabase()Permanently delete the current database and all its collections
adminBuilt-in database for administrative users and commands
localBuilt-in database for server-local data such as the replication oplog; never replicated
configBuilt-in database used by sharded clusters for metadata

Key Takeaways

use myapp does not create anything by itself. The database appears in show dbs only after you insert a document or create a collection in it.

Frequently Asked Questions

How do I create a database in MongoDB?

Run use dbName in mongosh, then insert a document or create a collection. The database is created when the first data is stored.

Why does show dbs not list my new database?

Databases without any data are not shown. Insert a document into a collection in that database and it will appear.

How do I delete a database?

Switch to it with use dbName and run db.dropDatabase(). This permanently removes all of its collections and data.

What are admin, local, and config?

They are built-in databases. admin holds administrative data and users, local holds server-specific data such as the oplog, and config holds sharded cluster metadata.

Can I have many databases on one server?

Yes. A single mongod can host many databases. Many teams still prefer fewer databases with well-named collections, depending on access control and operational needs.

Is a database name case-sensitive?

Yes. Names such as Blog and blog refer to different databases.

Next: MongoDB Collections

Continue the MongoDB learning path.

MongoDB Collections →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful