Create and Drop Databases
Learn how to create, switch between, list, and drop databases in MongoDB. Understand implicit database creation, the use database command, and how to safely remove databases you no longer need.
In MongoDB, databases are created implicitly — you don't need a "CREATE DATABASE" statement like in SQL. A database is created automatically when you first store data in it. This episode covers everything about creating, listing, switching, and dropping databases.
Creating a Database
To "create" a database, simply switch to it with the use command:
// Switch to a database (creates it if it doesn't exist)
use myDatabase
Output: switched to db myDatabase
Important: The database won't actually appear in show dbs until you insert at least one document into it. This is because MongoDB only allocates storage when data is written.
// This database exists in memory but won't show in 'show dbs' yet
use newProject
// Show databases — newProject won't appear
show dbs
// Insert a document to materialize the database
db.test.insertOne({ init: true })
// Now it appears
show dbs
Listing Databases
// Show all databases with their sizes
show dbs
// Alternative command
show databases
// Programmatic way — returns more details
db.adminCommand({ listDatabases: 1 })
The output of show dbs looks like:
admin 40.00 KiB
config 108.00 KiB
local 72.00 KiB
myDatabase 48.00 KiB
The three default databases are:
- admin — Used for administrative commands and authentication
- config — Stores configuration data for sharded clusters
- local — Stores data specific to a single server (replication oplog)
Checking the Current Database
// Show the current database name
db
// Get stats about the current database
db.stats()
// Get the database name programmatically
db.getName()
The db.stats() command returns useful information:
{
db: 'myDatabase',
collections: 3,
views: 0,
objects: 150,
avgObjSize: 128,
dataSize: 19200,
storageSize: 36864,
indexes: 3,
indexSize: 36864,
totalSize: 73728,
ok: 1
}
Dropping a Database
To permanently delete a database and all its data:
// Switch to the database you want to drop
use myDatabase
// Drop it
db.dropDatabase()
Output: { ok: 1, dropped: 'myDatabase' }
Warning: This operation is irreversible. There is no undo. All collections, documents, and indexes in the database will be permanently deleted. Always double-check you're on the correct database before dropping.
// Safety check — verify which database you're about to drop
db
// Output: myDatabase
// Only drop if you're sure
db.dropDatabase()
Database Naming Rules
MongoDB database names have these restrictions:
- Cannot be empty
- Cannot contain:
/,\,.,",*,<,>,:,|,?,$, null character, or spaces - Must be under 64 characters
- Are case-sensitive —
myDBandmydbare different databases - Convention: use camelCase or snake_case (e.g.,
ecommerce,user_data)
// Good database names
use ecommerce
use blogApp
use user_analytics
// Bad database names (will cause errors)
use my database // spaces
use my.database // dots
use my/database // slashes
Practical Example
Let's create a database for a blog application:
// Create and switch to the blog database
use blogApp
// Create an initial collection with a document
db.posts.insertOne({
title: "My First Post",
content: "Hello, MongoDB!",
author: "admin",
createdAt: new Date()
})
// Verify the database exists
show dbs
// Check database stats
db.stats()
What's Next
Now that you can create and manage databases, the next step is understanding collections — MongoDB's equivalent of tables. In the next episode, we'll learn how to create, list, and drop collections, and understand how they organize your documents.