🍃 MongoDB Tutorial

MongoDB Databases: Show, Create, Rename & Drop Database

Learn every MongoDB database command — show dbs, use to create or switch databases, how to rename a database, and dropDatabase() to delete one.

MongoDB Databases

Show Databases

Use show dbs (or show databases) to list every database on a MongoDB server, along with its size.

show databases
show dbs
admin   40.00 KiB
config  108.00 KiB
local   72.00 KiB

Create a Database

There's no explicit "create database" command. You switch to a database name with use — if it doesn't exist yet, MongoDB creates it as soon as you insert data into it.

use rahultech
// switched to db rahultech

db.emp.insertOne({ "name": "Rahul" })
// { acknowledged: true, insertedId: ObjectId("648f4c53c6c7840ea4a45e6b") }

show dbs
// rahultech now appears with a size, because it actually has data

A brand-new database with no documents in it won't show up in show dbs until something is inserted.

Rename a Database

MongoDB has no direct rename command for databases. The workaround is to dump the old database, restore it under a new name, then drop the original:

mongodump -d rahultech -o ./
mongorestore -d rahultechnology ./rahultech
show dbs
// rahultech         40.00 KiB
// rahultechnology   40.00 KiB

db.copyDatabase() used to do this in one step, but it was removed in MongoDB 4.0 — the dump-and-restore approach above is the current recommended way.

Delete / Drop a Database

db.dropDatabase() deletes the currently selected database. If no database is selected, it deletes the default test database instead.

use rahultech
db.dropDatabase()
// { ok: 1, dropped: 'rahultech' }

Common Errors & Troubleshooting

  • A newly created database doesn't show up in show dbs — MongoDB doesn't actually persist a database until it contains at least one document. Insert a document first, then check show dbs again.
  • db.dropDatabase() deleted the wrong database — this command always operates on the currently selected database, not one you name in the call. Run db (no parentheses) first to confirm which database you're connected to before dropping anything.
  • db.copyDatabase is not a function — this method was removed in MongoDB 4.0. Use the mongodump/mongorestore workaround shown above instead.

Key Takeaways

  • use dbname both switches to and lazily creates a database.
  • A database only "exists" visibly once it has at least one document.
  • To rename a database: dump → restore under the new name → drop the old one.
  • db.dropDatabase() permanently deletes the current database — there's no undo.