Lesson 41 of 50 – Student Management Database Project
82%

Student Management Database Using MongoDB

In this project, we will design a student management database using MongoDB. We will learn how to create student records, retrieve information, update student details, and delete records using MongoDB Shell.

1. Project Overview

This database can store information such as:

  • Student ID
  • Student name
  • Age and gender
  • Course name
  • Contact details
  • Address
  • Marks and grades
  • Admission date

MongoDB stores records as BSON documents. Each student will be represented by a document inside the students collection.

Database name: school

Collection name: students

2. Create the Database

Open MongoDB Shell:

mongosh

Switch to the school database:

use school

MongoDB selects the database. It is created on disk when data is first stored in it.

Create the students collection explicitly:

db.createCollection("students")

If the collection already exists, MongoDB will report an error. In that case, you can continue using it.

3. Design a Student Document

Example student document:

{
    student_id: 1001,
    name: "Rahul Kumar",
    age: 20,
    gender: "Male",
    course: "ADCA",
    mobile: "9876543210",
    email: "rahul@example.com",
    address: {
        city: "Aurangabad",
        state: "Bihar"
    },
    admission_date: ISODate("2026-04-10T00:00:00Z"),
    marks: {
        computer: 85,
        english: 78,
        mathematics: 90
    }
}

This document contains basic student information, an embedded address document, and subject-wise marks.

The field student_id is an application-level identifier. MongoDB also creates an automatic _id field for every inserted document unless you provide one yourself.

4. Insert One Student

Use insertOne() to insert one student:

db.students.insertOne({
    student_id: 1001,
    name: "Rahul Kumar",
    age: 20,
    gender: "Male",
    course: "ADCA",
    mobile: "9876543210",
    email: "rahul@example.com",
    address: {
        city: "Aurangabad",
        state: "Bihar"
    },
    admission_date: ISODate("2026-04-10T00:00:00Z"),
    marks: {
        computer: 85,
        english: 78,
        mathematics: 90
    }
})

MongoDB returns an acknowledgment and the inserted document's _id.

Run this insertion only once for the example, or use a different student ID for additional records.

5. Insert Multiple Students

Use insertMany() to add several student documents in one operation.

db.students.insertMany([
    {
        student_id: 1002,
        name: "Priya Singh",
        age: 19,
        gender: "Female",
        course: "Web Development",
        mobile: "9876543211",
        city: "Patna",
        marks: {
            computer: 92,
            english: 88
        }
    },
    {
        student_id: 1003,
        name: "Amit Kumar",
        age: 21,
        gender: "Male",
        course: "Tally Prime",
        mobile: "9876543212",
        city: "Gaya",
        marks: {
            computer: 75,
            english: 80
        }
    },
    {
        student_id: 1004,
        name: "Neha Kumari",
        age: 20,
        gender: "Female",
        course: "ADCA",
        mobile: "9876543213",
        city: "Aurangabad",
        marks: {
            computer: 89,
            english: 91
        }
    }
])

This example assumes these student IDs are not already present. Avoid rerunning the insertion unchanged if you want to prevent duplicate sample records.

6. Display All Students

Retrieve every student document:

db.students.find()

Display records in a readable format:

db.students.find().pretty()

Display only selected fields:

db.students.find(
    {},
    {
        _id: 0,
        student_id: 1,
        name: 1,
        course: 1
    }
)

The projection includes the student ID, name, and course while excluding the automatic MongoDB _id field.

7. Search for a Student

Find a student using the application-level ID:

db.students.findOne({
    student_id: 1001
})

Find students enrolled in ADCA:

db.students.find({
    course: "ADCA"
})

Find students older than 19:

db.students.find({
    age: { $gt: 19 }
})

Find students from Aurangabad:

db.students.find({
    "address.city": "Aurangabad"
})

The dot notation allows you to query a field inside an embedded document, such as address.city.

8. Update Student Details

Use updateOne() with $set to change specific fields.

db.students.updateOne(
    { student_id: 1001 },
    {
        $set: {
            age: 21,
            course: "Full Stack Development"
        }
    }
)

Update the student's city:

db.students.updateOne(
    { student_id: 1001 },
    {
        $set: {
            "address.city": "Patna"
        }
    }
)

MongoDB changes only the specified fields. Other fields in the document remain unchanged.

9. Update Student Marks

Update the computer marks for a student:

db.students.updateOne(
    { student_id: 1001 },
    {
        $set: {
            "marks.computer": 95
        }
    }
)

Display the updated record:

db.students.findOne({
    student_id: 1001
})

Dot notation updates the nested computer field inside the marks document.

10. Delete a Student

Use deleteOne() to delete one student matching a filter.

db.students.deleteOne({
    student_id: 1004
})

MongoDB returns a result showing whether a document was deleted.

Warning: Deleting a document removes it from the collection. Check the filter carefully before executing deletion commands.

To count the remaining student documents, use:

db.students.countDocuments({})

11. Count and Sort Students

Count all student records:

db.students.countDocuments({})

Count students enrolled in ADCA:

db.students.countDocuments({
    course: "ADCA"
})

Sort students by name in ascending order:

db.students.find().sort({
    name: 1
})

Display the five oldest students:

db.students.find()
    .sort({ age: -1 })
    .limit(5)

A sort value of 1 means ascending order, while -1 means descending order.

12. Prevent Duplicate Student IDs

If every student must have a unique student_id, create a unique index.

db.students.createIndex(
    { student_id: 1 },
    { unique: true }
)

This prevents MongoDB from accepting two documents with the same indexed student ID.

If duplicate IDs already exist, index creation can fail. Check and resolve duplicates before creating the index.

A unique index is useful when student IDs are assigned by the application rather than relying solely on MongoDB's automatically generated _id.

13. Mini Project: Student Reports

Find all students enrolled in ADCA:

db.students.find({
    course: "ADCA"
})

Find students whose computer marks are at least 80:

db.students.find({
    "marks.computer": { $gte: 80 }
})

Count students by course using aggregation:

db.students.aggregate([
    {
        $group: {
            _id: "$course",
            totalStudents: { $sum: 1 }
        }
    },
    {
        $sort: {
            totalStudents: -1
        }
    }
])

The aggregation groups documents by course and counts the number of students in each group.

14. Practical Assignment

  1. Create the school database and students collection.
  2. Insert at least five student documents.
  3. Include each student's ID, name, age, course, city, and marks.
  4. Display all students using find().
  5. Find students enrolled in a specific course.
  6. Find students whose age is greater than 18.
  7. Update one student's course and marks.
  8. Sort students by name.
  9. Count students enrolled in each course using aggregation.
  10. Create a unique index on student_id.
  11. Delete a test student and verify the result.

🧠 Quick Quiz

1. Which method inserts one student document?




2. Which operator updates selected fields?




3. Which option enforces unique student IDs?