In MongoDB, a database stores collections, and collections store documents. In this lesson, you will learn how to select a database, create a database by inserting data, display databases, and remove a database when it is no longer needed.
A database is a container used to organize related collections. Each collection contains documents that store information.
For example, a school database might contain:
school_db
|
|-- students
|-- teachers
|-- courses
|-- fees
First, make sure your MongoDB server is running. Open Command Prompt or PowerShell and enter:
mongosh
If the shell connects successfully, you can execute MongoDB commands.
If MongoDB Shell is already open, you can continue with the next step.
MongoDB uses the use command to select
a database.
Syntax:
use database_name
Example:
use school_db
Check the selected database:
db
Expected output:
school_db
use command
selects a database but does not necessarily create it
permanently. MongoDB creates the database when data is
first stored or a collection is explicitly created.
One common way to create a database is to select its name and then insert a document into a collection.
Step 1: Select the database
use school_db
Step 2: Insert a student document
db.students.insertOne({
name: "Rahul",
age: 20,
course: "ADCA",
city: "Aurangabad"
})
This command inserts one document into the
students collection. If the collection and
database do not exist, MongoDB creates them as needed.
Step 3: Display the inserted document
db.students.find()
You can also explicitly create a collection in the selected database.
Step 1: Select the database
use college_db
Step 2: Create a collection
db.createCollection("students")
Step 3: Display collections
show collections
Expected output includes:
students
Creating a collection explicitly creates the database if it does not already exist.
Use the show dbs command to display databases
visible to your MongoDB connection.
show dbs
MongoDB displays database names and their reported sizes.
A database that has only been selected with
use might not appear until data or a
collection has been created.
You can switch to another database at any time using
the use command.
Select the school database:
use school_db
Check the current database:
db
Switch to the library database:
use library_db
The db variable refers to the database
currently selected in the shell.
The dropDatabase() method removes the
currently selected database.
Syntax:
db.dropDatabase()
Example:
use practice_db
db.dropDatabase()
This deletes practice_db and its data if
the operation succeeds.
Run the following commands one by one to create a database for a computer training institute.
Step 1: Select the database
use soopro_db
Step 2: Create the students collection
db.createCollection("students")
Step 3: Insert a student
db.students.insertOne({
student_id: 101,
name: "Amit Kumar",
course: "Web Development",
feesPaid: 5000
})
Step 4: Display the student
db.students.find()
Step 5: Check the database list
show dbs
You have now selected a database, created a collection, inserted a document, and displayed the stored data.
training_db.courses.library_db.db.Q1. Which command selects a database named school_db?
Q2. Which command displays available databases?
Q3. Which method deletes the currently selected database?