Mongo Document
Mongo Document
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.
1. Efficient Data Management: To provide a structured way to store and manage data
related to students, teachers, and courses.
Project Goals:
Document and present the project in a way that showcases its functionality and
potential applications.
---
Database Structure
Collections Overview:
1. Students Collection:
Stores detailed information about students, including personal details, contact
information, enrolled courses, and academic performance.
2. Teachers Collection:
3. Courses Collection:
Includes course details such as the course name, description, credits, and the
students enrolled.
---
Students Collection:
{
"_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:
courses: Array of course ObjectId references to link with the Courses collection.
email: Contact email address.
---
Teachers Collection:
{
"_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:
---
Courses Collection:
{
"_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:
---
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.
---
To add new records to the database, we use the insertOne() and insertMany() methods
for inserting a single document or multiple documents, respectively.
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"
});
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"
}
]);
---
MongoDB provides methods such as find() to retrieve data from collections. This
method allows filtering, sorting, and projection of specific fields.
---
db.students.updateOne(
{ "name": "Alice Johnson" },
{ $set: { "gpa": 3.9 } }
);
db.teachers.updateOne(
{ "name": "William Carter" },
{ $set: { "salary": 60000 } }
);
db.students.updateOne(
{ "name": "John Doe" },
{ $push: { "courses": { "$oid": "60c72c4e4f1a4e3d88f7e718" } } }
);
db.students.updateMany(
{ "class": "10th Grade" },
{ $set: { "gpa": 3.8 } }
);
---
db.courses.updateOne(
{ "name": "Introduction to Biology" },
{ $pull: { "studentsEnrolled": { "$oid": "60c72b2f4f1a4e3d88f7e5f1" } } }
);
---
Let's say we want to retrieve the list of all students who are enrolled in the
"Mathematics" course.
(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.)
---
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.
---
7. Future Enhancements
---
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.
---