Niit Campus Technology LTD
Niit Campus Technology LTD
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.
2.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:
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:
"age": 15,
"sex": "Female",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e702" },
{ "$oid": "60c72c4e4f1a4e3d88f7e704" }
],
"email": "alicejohnson@gmail.com",
"phone": "08151234091",
"gpa": 3.5,
"enrollmentDate": "2024-01-01"
Fields Explanation:
courses: Array of course ObjectId references to link with the Courses collection.
Teachers Collection:
"title": "Mr.",
"age": 40,
"gender": "Male",
"subject": "Mathematics",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e701" },
{ "$oid": "60c72c4e4f1a4e3d88f7e718" }
],
"email": "williamcarter@gmail.com",
"phone": "09154010923",
"yearsOfExperience": 15,
"salary": 55000,
"hiredDate": "2023-06-21"
Fields Explanation:
Courses Collection:
"credits": 3,
"department": "Science",
"teacherId": [
{ "$oid": "60c72b3f4f1a4e3d88f7e702" },
{ "$oid": "60c72b3f4f1a4e3d88f7e707" }
],
"studentsEnrolled": [
{ "$oid": "60c72b2f4f1a4e3d88f7e5f2" },
{ "$oid": "60c72b2f4f1a4e3d88f7e604" }
Fields Explanation:
In this database, the collections are related to each other using ObjectIds. This creates references
between documents in different collections. The relationships are as follows:
1. Student to Courses:
Each student can be enrolled in multiple courses. This relationship is established by referencing the
courses collection in the students collection. The courses field in a student document contains an array
of ObjectIds, each representing a course the student is enrolled in.
2. Teacher to Courses:
Each teacher can teach multiple courses, and each course can have multiple teachers. The courses field
in the teachers collection contains an array of ObjectIds referencing the courses taught by that teacher.
Similarly, the teacherId field in the courses collection references the teachers who teach that particular
course.
3. Course to Students:
Each course can have multiple students enrolled. This relationship is established by referencing the
students collection in the courses collection. The studentsEnrolled field in a course document contains
an array of ObjectIds referencing the students who are enrolled in the course.
4.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.
db.students.insertOne({
"age": 16,
"sex": "Male",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e702" },
{ "$oid": "60c72c4e4f1a4e3d88f7e704" }
],
"email": "johndoe@gmail.com",
"phone": "08033456789",
"gpa": 3.7,
"enrollmentDate": "2024-01-15"
});
db.teachers.insertMany([
"title": "Ms.",
"age": 35,
"gender": "Female",
"subject": "Physics",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e701" }
],
"email": "sarahwilliams@gmail.com",
"phone": "09122223333",
"yearsOfExperience": 10,
"salary": 50000,
"hiredDate": "2023-05-21"
},
{
"title": "Mr.",
"age": 50,
"gender": "Male",
"courses": [
{ "$oid": "60c72c4e4f1a4e3d88f7e718" }
],
"email": "jamesbrown@gmail.com",
"phone": "09123334444",
"yearsOfExperience": 20,
"salary": 60000,
"hiredDate": "2015-01-09"
]);
db.students.updateOne(
);
db.teachers.updateOne(
);
db.students.updateOne(
{ "name": "John Doe" },
);
db.students.updateMany(
);
To delete documents from a collection, we use deleteOne(), deleteMany(), or drop(). These methods
allow for the removal of specific or all documents.
db.courses.updateOne(
);
Let's say we want to retrieve the list of all students who are enrolled in the "Mathematics" course.
This will include the students, teachers, and courses collections, and how they relate to each other.
6. How to Use the Database: A Step-by-Step Guide
This section provides a comprehensive guide on how to interact with the MongoDB School Database
using basic CRUD (Create, Read, Update, Delete) operations. It also explains how to set up, query, and
manage data efficiently.
1. Install MongoDB: Ensure MongoDB is installed on your system. Download and install it from the
official MongoDB website.
2. Start the MongoDB Server: Run the following command in the terminal to start the MongoDB service:
‘mongod’
‘mongo’
This opens the MongoDB shell, where you can run MongoDB commands directly.
‘use schoolDatabase’
2. Adding Data to the Collections (Create Operation)
To add a new student, use the following command in the MongoDB shell or in a script:
db.students.insertOne({
age: 16,
sex: "Male",
courses: [
ObjectId("60c72c4e4f1a4e3d88f7e700"),
ObjectId("60c72c4e4f1a4e3d88f7e704")
],
email: "johndoe@gmail.com",
phone: "08123456789",
gpa: 3.8,
Enrolled_Date: "15-01-2024"
});
db.teachers.insertOne({
title: "Dr.",
age: 42,
gender: "Female",
subject: "Chemistry",
courses: [
ObjectId("60c72c4e4f1a4e3d88f7e701"),
ObjectId("60c72c4e4f1a4e3d88f7e710")
],
email: "janesmith@school.edu",
phone: "09012345678",
yearsOfExperience: 18,
salary: 60000,
Hired_Date: "05-09-2019"
});
db.students.find().pretty();
This command fetches all documents in the students collection and displays them in a readable format.
Use an aggregation pipeline to join the students collection with the courses collection:
db.students.aggregate([
$lookup: {
from: "courses",
localField: "courses",
foreignField: "_id",
as: "enrolledCourses"
]);
db.students.updateOne(
);
db.teachers.updateOne(
);
1. Data Validation: Ensure all data entered follows the expected format (e.g., valid email
addresses, correct ObjectId references).
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.