All projects
School Management
Students, courses, enrollments, and grade analytics.
Schema design
students
Contact info embedded; enrollments referenced (unbounded, queried separately).
students document
{
_id: ObjectId(),
name: "Meera Nair", year: 10,
guardian: { name: "R. Nair", phone: "+91..." }
}enrollments
Junction collection: student ↔ course with embedded grade data.
enrollments document
{
_id: ObjectId(),
studentId: ObjectId(), courseId: ObjectId(),
term: "2026-S1",
scores: [ { assessment: "midterm", score: 84 },
{ assessment: "final", score: 91 } ]
}Key queries
A student's courses this term
mongosh
db.enrollments.find({ studentId: sid, term: "2026-S1" })Class roster
mongosh
db.enrollments.aggregate([
{ $match: { courseId: cid, term: "2026-S1" } },
{ $lookup: { from: "students", localField: "studentId",
foreignField: "_id", as: "student" } },
{ $unwind: "$student" },
{ $project: { "student.name": 1 } }
])Indexes
db.enrollments.createIndex({ studentId: 1, term: 1 })Per-student timetable lookups.
db.enrollments.createIndex({ courseId: 1, term: 1 })Per-course rosters and grading.
Aggregations
Course averages
mongosh
db.enrollments.aggregate([
{ $unwind: "$scores" },
{ $group: { _id: "$courseId",
avgScore: { $avg: "$scores.score" } } },
{ $sort: { avgScore: -1 } }
])Performance tips
- 1.Enrollment is a classic many-to-many — a junction collection with two compound indexes serves both directions.
- 2.Embed scores inside the enrollment: they're bounded and always read together.
Want to experiment with these patterns? Open the playground and build the collections yourself, or practice related skills in the challenges.