Lesson 6 of 50 – Creating a Database in MongoDB
12%

Creating a Database in MongoDB

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.

1. What Is a Database in MongoDB?

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
  • Database: school_db
  • Collection: students
  • Document: One student's record

2. Open MongoDB Shell

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.

3. Select a Database Using use

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
Important: The 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.

4. Create a Database by Inserting Data

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()

5. Create a Database Using createCollection()

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.

6. Display All Databases

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.

Database visibility and access depend on your user's permissions and the server configuration.

7. Switch Between Databases

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.

8. Drop a Database

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.

Warning: Dropping a database can permanently remove its collections and data. Only run this command on a database you intentionally want to delete. Do not use it on important or production databases.

9. Complete Practical Example

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.

10. Practice Assignment

  1. Create or select a database named training_db.
  2. Create a collection named courses.
  3. Insert a document containing a course name, duration, and fee.
  4. Display all databases.
  5. Display all collections in the current database.
  6. Switch to a database named library_db.
  7. Check the current database using db.
  8. Explain the difference between selecting and creating a database.

🧠 Quick Quiz

Q1. Which command selects a database named school_db?




Q2. Which command displays available databases?




Q3. Which method deletes the currently selected database?