MongoDB Create Database

Learn how to create and manage databases in MongoDB

🗄️ Creating Databases in MongoDB

In MongoDB, databases are created automatically when you first store data in them. You don't need to explicitly create a database beforehand. Simply use the database and MongoDB creates it for you instantly.


// Switch to a new database (creates it automatically)
use myNewDatabase

// MongoDB creates the database when you add data
db.users.insertOne({ name: "John" })
                                    

Understanding Database Creation

MongoDB uses a lazy creation approach for databases. The database appears only after you insert your first document into a collection. This makes database management simple and efficient for beginners.

🎯

Automatic Creation

No explicit CREATE command needed

use shopDB
📝

Data Triggers Creation

Database appears when data is added

db.products.insertOne({})
👁️

View Databases

List all existing databases

show dbs
🔄

Switch Databases

Change active database anytime

use anotherDB

🔹 Using the use Command

The use command switches to a database. If it doesn't exist, MongoDB prepares to create it when you add data:

// Switch to database (creates if doesn't exist)
use schoolDB

// Check current database
db

// Output: schoolDB

Output:

schoolDB

🔹 Creating Database with Data

To actually create the database, insert at least one document into a collection:

// Switch to new database
use libraryDB

// Insert data (this creates the database)
db.books.insertOne({
    title: "MongoDB Basics",
    author: "John Doe",
    year: 2024
})

// Verify database was created
show dbs

Output:

admin    0.000GB

config   0.000GB

libraryDB 0.000GB

local    0.000GB

🔹 Viewing All Databases

Use the show dbs command to list all databases with data:

// List all databases
show dbs

// Alternative command
show databases

Note:

Empty databases (without collections) won't appear in the list. You must insert at least one document for the database to be visible.

🔹 Complete Example

Here's a complete workflow for creating and using a database:

// Step 1: Switch to new database
use companyDB

// Step 2: Create a collection with data
db.employees.insertOne({
    name: "Alice Smith",
    position: "Developer",
    salary: 75000
})

// Step 3: Verify database exists
show dbs

// Step 4: Check current database
db

// Step 5: View collections in database
show collections

Output:

✓ Database 'companyDB' created

✓ Collection 'employees' created

✓ Document inserted successfully

🔹 Database Naming Rules

Follow these rules when naming your MongoDB databases:

  • Case Sensitive: "MyDB" and "mydb" are different
  • No Spaces: Use underscores or camelCase
  • Max Length: 64 characters maximum
  • Avoid Special Characters: No /, \, ., ", *, <, >, :, |, ?, $
  • Reserved Names: Don't use "admin", "local", "config"
// Good database names
use myAppDB
use user_data
use ecommerce2024

// Bad database names (avoid these)
use my database  // has space
use my/db        // has special character
use admin        // reserved name

🧠 Test Your Knowledge

When is a MongoDB database actually created?