0% found this document useful (0 votes)
17 views9 pages

Mongo Document

Uploaded by

Abdulsamad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Mongo Document

Uploaded by

Abdulsamad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction

Overview of the Project:

The School Database project is designed to manage and organize key academic data
efficiently using MongoDB, a NoSQL database. It includes three main collections:
Students, Teachers, and Courses. Each collection contains detailed information and
is interconnected through unique identifiers (ObjectId), forming a robust data
model suitable for educational institutions.

Objectives and Purpose:

1. Efficient Data Management: To provide a structured way to store and manage data
related to students, teachers, and courses.

2. Interconnected Data: Establish relationships between collections for dynamic


data retrieval, enabling queries like “Which students are enrolled in a particular
course?” or “Which courses does a specific teacher instruct?”

3. Scalability: Use MongoDB’s document model to handle large volumes of data,


ensuring flexibility and scalability.

4. User Interaction: Support administrative tasks such as viewing, adding, or


modifying data related to students, teachers, and courses.

Project Goals:

Store detailed data for each student, teacher, and course.

Establish and demonstrate relationships between collections using ObjectId


references.

Provide examples of CRUD (Create, Read, Update, Delete) operations to manipulate


the data effectively.

Document and present the project in a way that showcases its functionality and
potential applications.

---

Database Structure

Collections Overview:

The database consists of three primary collections:

1. Students Collection:
Stores detailed information about students, including personal details, contact
information, enrolled courses, and academic performance.

2. Teachers Collection:

Contains information about teachers, including their subjects, experience, contact


details, and the courses they teach.

3. Courses Collection:

Includes course details such as the course name, description, credits, and the
students enrolled.

---

Students Collection:

Here is the structure of a typical student document:

{
"_id": { "$oid": "60c72b2f4f1a4e3d88f7e5f1" },
"name": "Alice Johnson",
"age": 15,
"sex": "Female",
"class": "10th Grade",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e702" },
{ "$oid": "60c72c4e4f1a4e3d88f7e704" }
],
"email": "[email protected]",
"address": "No 23, Aco Estate, Main-de Street, Abuja.",
"phone": "08151234091",
"gpa": 3.5,
"guardianName": "Emma Johnson",
"enrollmentDate": "2024-01-01"
}

Fields Explanation:

_id: Unique identifier for each student.

name: Full name of the student.

age: Age of the student.

sex: Gender of the student.

class: Current grade or class level.

courses: Array of course ObjectId references to link with the Courses collection.
email: Contact email address.

address: Residential address.

phone: Contact phone number.

gpa: Grade Point Average, indicating academic performance.

guardianName: Name of the student's guardian.

enrollmentDate: Date the student enrolled.

---

Teachers Collection:

Example teacher document structure:

{
"_id": { "$oid": "60c72b3f4f1a4e3d88f7e701" },
"title": "Mr.",
"name": "William Carter",
"age": 40,
"gender": "Male",
"subject": "Mathematics",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e701" },
{ "$oid": "60c72c4e4f1a4e3d88f7e718" }
],
"email": "[email protected]",
"address": "No 12, Gwarinpa Estate, 3rd Avenue, Abuja.",
"phone": "09154010923",
"yearsOfExperience": 15,
"salary": 55000,
"hiredDate": "2023-06-21"
}

Fields Explanation:

_id: Unique identifier for each teacher.

title: Salutation (Mr., Ms., Dr., etc.).

name: Full name of the teacher.

age: Age of the teacher.

gender: Gender of the teacher.

subject: Main subject taught by the teacher.

courses: Array of course ObjectId references linking to the Courses collection.

email: Contact email address.

address: Residential address.


phone: Contact phone number.

yearsOfExperience: Total teaching experience in years.

salary: Monthly salary of the teacher.

hiredDate: Date when the teacher was hired.

---

Courses Collection:

Example course document structure:

{
"_id": { "$oid": "60c72c4e4f1a4e3d88f7e700" },
"name": "Introduction to Biology",
"description": "An overview of basic biological principles.",
"credits": 3,
"department": "Science",
"teacherId": [
{ "$oid": "60c72b3f4f1a4e3d88f7e702" },
{ "$oid": "60c72b3f4f1a4e3d88f7e707" }
],
"semester": "2nd Semester 2024",
"studentsEnrolled": [
{ "$oid": "60c72b2f4f1a4e3d88f7e5f2" },
{ "$oid": "60c72b2f4f1a4e3d88f7e604" }
]
}

Fields Explanation:

_id: Unique identifier for each course.

name: Name of the course.

description: Brief overview of the course.

credits: Credit value of the course.

department: Department offering the course (e.g., Science, Arts).

teacherId: Array of ObjectId references to teachers teaching the course.

semester: Semester in which the course is offered.

studentsEnrolled: Array of ObjectId references to enrolled students.

---

CRUD Operations

In this section, we'll discuss the basic CRUD (Create, Read, Update, Delete)
operations for interacting with the MongoDB database. These operations are
fundamental for managing data in our school management system.

---

1. Create Operation (Insert Data)

To add new records to the database, we use the insertOne() and insertMany() methods
for inserting a single document or multiple documents, respectively.

Inserting a New Student:

db.students.insertOne({
"name": "John Doe",
"age": 16,
"sex": "Male",
"class": "11th Grade",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e702" },
{ "$oid": "60c72c4e4f1a4e3d88f7e704" }
],
"email": "[email protected]",
"address": "No 5, Akwa Street, Lagos.",
"phone": "08033456789",
"gpa": 3.7,
"guardianName": "Jane Doe",
"enrollmentDate": "2024-01-15"
});

Inserting Multiple Teachers:

db.teachers.insertMany([
{
"title": "Ms.",
"name": "Sarah Williams",
"age": 35,
"gender": "Female",
"subject": "Physics",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e701" }
],
"email": "[email protected]",
"address": "No 4, Sapele Road, Benin.",
"phone": "09122223333",
"yearsOfExperience": 10,
"salary": 50000,
"hiredDate": "2023-05-21"
},
{
"title": "Mr.",
"name": "James Brown",
"age": 50,
"gender": "Male",
"subject": "English Literature",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e718" }
],
"email": "[email protected]",
"address": "No 10, Broad Street, Lagos.",
"phone": "09123334444",
"yearsOfExperience": 20,
"salary": 60000,
"hiredDate": "2015-01-09"
}
]);

---

2. Read Operation (Retrieve Data)

MongoDB provides methods such as find() to retrieve data from collections. This
method allows filtering, sorting, and projection of specific fields.

Finding a Specific Student:

db.students.find({ "name": "Alice Johnson" });

Finding Students Enrolled in a Specific Course:

db.students.find({ "courses": { "$in": [{ "$oid":


"60c72c4e4f1a4e3d88f7e702" }] } });

Finding Teachers Who Teach a Specific Subject:

db.teachers.find({ "subject": "Mathematics" });

Finding Courses Taught by a Specific Teacher:

db.courses.find({ "teacherId": { "$in": [{ "$oid":


"60c72b3f4f1a4e3d88f7e701" }] } });

---

3. Update Operation (Modify Data)

To modify existing documents in MongoDB, we use updateOne(), updateMany(), or


replaceOne(). These methods allow you to update one or more documents that match a
specific filter.

Updating a Student’s GPA:

db.students.updateOne(
{ "name": "Alice Johnson" },
{ $set: { "gpa": 3.9 } }
);

Updating a Teacher's Salary:

db.teachers.updateOne(
{ "name": "William Carter" },
{ $set: { "salary": 60000 } }
);

Adding a New Course to a Student’s Enrollment:

db.students.updateOne(
{ "name": "John Doe" },
{ $push: { "courses": { "$oid": "60c72c4e4f1a4e3d88f7e718" } } }
);

Updating Multiple Courses Enrolled by Students:

db.students.updateMany(
{ "class": "10th Grade" },
{ $set: { "gpa": 3.8 } }
);

---

4. Delete Operation (Remove Data)

To delete documents from a collection, we use deleteOne(), deleteMany(), or drop().


These methods allow for the removal of specific or all documents.

Deleting a Student Record:

db.students.deleteOne({ "name": "Alice Johnson" });

Deleting a Teacher Record:

db.teachers.deleteOne({ "name": "William Carter" });

Removing a Student from a Course:

db.courses.updateOne(
{ "name": "Introduction to Biology" },
{ $pull: { "studentsEnrolled": { "$oid": "60c72b2f4f1a4e3d88f7e5f1" } } }
);

Deleting All Students from a Specific Grade:

db.students.deleteMany({ "class": "10th Grade" });

---

Example Query Execution:

Let's say we want to retrieve the list of all students who are enrolled in the
"Mathematics" course.

1. Find the course:

db.courses.find({ "name": "Mathematics" });

This will give us the ObjectId of the course.

2. Find all students enrolled in this course:

db.students.find({ "courses": { "$in": [ObjectId("60c72c4e4f1a4e3d88f7e702")] } });


---

5. Database Schema Design Diagram

(You can later add a visual schema diagram here to represent the relationships
visually. This will include the students, teachers, and courses collections, and
how they relate to each other.)

---

6. How to Use the Database

1. Adding New Data: Use MongoDB's insertOne() or insertMany() methods to add new
documents to any collection. You can use Node.js or MongoDB's shell to interact
with the database.

2. Fetching Data: Use MongoDB’s find() or aggregate() methods to query data. For
example, to find all students in a specific class or to get all the students
enrolled in a particular course.

3. Updating Data: Use MongoDB’s updateOne() or updateMany() methods to modify


existing data, such as adding a new course to a student’s enrollment.

4. Deleting Data: Use MongoDB’s deleteOne() or deleteMany() methods to remove


documents from any collection.

---

7. Future Enhancements

1. Admin Panel: A web-based interface to manage the students, teachers, and


courses.

2. Authentication: Implementing a user authentication system for teachers and


students.

3. Reporting: Adding functionality to generate reports, such as a student’s GPA,


teacher’s salary, or course enrollment statistics.

---

8. Conclusion

This MongoDB School Database project is an efficient way to store and manage school
data. Using MongoDB’s flexible document-based structure, it can handle complex data
relationships with ease. The use of references (ObjectIds) allows for efficient
querying and maintains the integrity of the data.

---

You might also like