Before creating a database, ensure MongoDB is running. Open the Command Prompt (Windows) or Terminal (macOS/Linux) and type:
mongo
To list all available databases, run:
show databases
Unlike traditional databases, MongoDB does not use an explicit CREATE DATABASE
command. Instead, you switch to a database using the use
command:
use myDatabase
MongoDB creates the database only when it has at least one collection with data. To create a collection and insert data, use:
db.users.insertOne({ name: "Alice", age: 25 })
Now, check if your database is created:
show databases
Example: Creating Another Database with Multiple Documents
use myNewDB
db.products.insertMany([
{ name: "Laptop", price: 1000 },
{ name: "Mouse", price: 50 },
{ name: "Keyboard", price: 70 }
])
show databases
You have successfully created databases in MongoDB. Remember:
✅ MongoDB creates a database only when data is added.
✅ Use show databases
to list all databases.
✅ Use db
to check the current database.
Now, you can start building applications with MongoDB! 🚀