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.
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.
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.
show dbs
This command lists all databases that have at least one document stored.
use myNewDB
but donβt insert any data, it wonβt appear in show dbs
output.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!