🧩 MongoDB Practice Series – Full Real-World Project Challenge (Part 8)
Welcome to Part 8 of the MongoDB Practice Series.
This is a real-world project practice where you will combine everything you learned:
- CRUD operations
- Filtering
- Sorting
- Aggregation
- Schema design
- Indexing
This challenge simulates building a real application database.
🚀 Project: Online Learning Platform Database
You are designing a database for an online learning website.
The system must manage:
- Users (students)
- Courses
- Enrollments
- Payments
🟢 Step 1: Design the Schema
Problem:
Design collections for the system.
Solution Example:
// Users Collection
{
_id: 1,
name: "Amit",
email: "amit@example.com",
country: "India"
}
// Courses Collection
{
_id: 101,
title: "MongoDB Basics",
price: 1500,
category: "Database"
}
// Enrollments Collection
{
_id: 5001,
user_id: 1,
course_id: 101,
enrollmentDate: new Date()
}
// Payments Collection
{
_id: 9001,
user_id: 1,
amount: 1500,
paymentStatus: "Completed"
}
Learning Goal: Understand when to use referencing for scalability.
🟡 Step 2: Basic Operations
Task:
- Insert 5 users
- Insert 5 courses
- Create enrollments
Practice: Use insertMany() for efficiency.
🔵 Step 3: Real-World Queries
✔ Find all courses in a category
db.courses.find({ category: "Database" })
✔ Find users from India
db.users.find({ country: "India" })
✔ Show latest enrollments
db.enrollments.find()
.sort({ enrollmentDate: -1 })
.limit(5)
🟣 Step 4: Aggregation Practice
✔ Count total enrollments per course
db.enrollments.aggregate([
{
$group: {
_id: "$course_id",
totalEnrollments: { $sum: 1 }
}
}
])
✔ Calculate total revenue
db.payments.aggregate([
{
$match: { paymentStatus: "Completed" }
},
{
$group: {
_id: null,
totalRevenue: { $sum: "$amount" }
}
}
])
🔴 Step 5: Performance Optimization
Create indexes for better performance:
// Faster user search
db.users.createIndex({ email: 1 })
// Faster enrollment lookup
db.enrollments.createIndex({ user_id: 1 })
// Faster course filtering
db.courses.createIndex({ category: 1 })
Learning Goal: Improve query performance in large systems.
🎯 Final Challenge (Capstone Task)
Design a reporting query that:
- Finds total revenue per course
- Sorts courses by highest revenue
- Returns only top 3 courses
Try solving it by combining:
- $group
- $sort
- $limit
This is a real interview-level challenge.
🏆 Congratulations!
If you completed all 8 parts of this MongoDB Practice Series, you now understand:
- CRUD
- Filtering
- Sorting
- Pagination
- Aggregation
- Schema Design
- Indexing
- Real-world database design
You are now ready to build real MongoDB applications.
No comments:
Post a Comment