MONGODB Tutorial



MongoDB CREATE DATABASE


πŸ› οΈ Creating a Database in MongoDB

In MongoDB, a database is created automatically when you insert data into a collection within it. There is no explicit create database command like in SQL.

πŸ”Ή Syntax to Create a Database

Use the use command to switch to a new or existing database:

use myDatabase
  

This command switches the current database context to myDatabase. If it doesn’t exist, MongoDB creates it when you insert the first document.

πŸ“₯ Example: Create a Database with a Collection

use myCompanyDB

db.employees.insertOne({
  name: "Ravi Kumar",
  department: "Sales",
  age: 30
});
  

After inserting a document, the database myCompanyDB and the collection employees are both created.

πŸ” View Existing Databases

show dbs
  

This command lists all databases that have at least one document stored.

πŸ’‘ Note: If you run use myNewDB but don’t insert any data, it won’t appear in show dbs output.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review