Chapter 28: MongoDB Exercises

What exactly are “MongoDB Exercises”?

MongoDB Exercises are deliberate, hands-on practice problems designed to make you:

  • Write real queries (find, aggregate, update, delete)
  • Use operators, pipelines, joins ($lookup), indexes
  • Handle real-world messy data patterns
  • Debug errors yourself
  • Build muscle memory so that when you’re in a job interview / building a side project / fixing production bugs → your fingers just know what to type

Think of them as:

  • Gym workouts for your brain → repetition builds strength
  • LeetCode / HackerRank but focused only on MongoDB (not generic algorithms)
  • The difference between “I read how to swim” vs “I actually jumped in the pool”

Why are exercises so important in MongoDB? (Teacher speaking from heart)

MongoDB looks simple at first (“it’s just JSON!”), but real mastery comes from:

  • Remembering dozens of operators ($elemMatch vs $all vs $in)
  • Knowing when to $unwind before $group
  • Understanding why $match first saves 10× time
  • Handling nested arrays/objects without panic
  • Writing $lookup + $unwind + $group without looking at docs every time
  • Spotting when an index is missing just by looking at .explain()

You cannot get this fluency just by watching YouTube videos or reading docs — you have to type, break things, fix them, type again.

Structure of Good MongoDB Exercises (what we’ll follow)

Level → Beginner / Intermediate / Advanced Goal → clear Data → small sample dataset (you can insert it yourself) Tasks → 5–8 questions increasing in difficulty Expected output → shown so you can self-check Bonus → “make it production-ready” hints

Let’s do one complete set right now — copy-paste ready.

Exercise Set #1: Students & Marks (Beginner → Intermediate)

Step 1: Create & populate the collection

Open mongosh (or MongoDB Compass shell):

JavaScript

Step 2: Exercises — solve them one by one

Level: Beginner

  1. Find all students from Hyderabad.
  2. Find students who have “coding” in their hobbies.
  3. Find students with math marks greater than 90.
  4. Count how many students are active (isActive: true).

Level: Intermediate

  1. Find students in grade “10th” who are from Hyderabad and have math ≥ 85 or science ≥ 85.
  2. Show only name, city, and total marks (sum of all subjects). Hint: use $addFields + $sum on object values.
  3. List all unique hobbies across all students. Hint: $unwind → $group → $addToSet
  4. Find the student with the highest math score (show name & score).

Level: Advanced

  1. Calculate average math score per city (round to 2 decimals).
  2. For each city, show:
    • number of students
    • average age
    • highest math score
    • list of student names
  3. Add a new field “performance” = “Excellent” if math ≥ 90, “Good” if ≥ 80, else “Needs Improvement” Then show name, city, math, performance.
  4. Create a ranked list: top 3 students by total marks (sum all subjects).

Answers / Solutions (check after trying yourself)

I’ll give one example solution for #9 (average math per city):

JavaScript

7. Where to Find More Great MongoDB Exercises (2026 recommendations)

  • Official MongoDB University — free courses with built-in exercises → https://learn.mongodb.com → “MongoDB Basics”, “Aggregation”, “Atlas Search”
  • MongoDB Developer Hub — practice labs → https://www.mongodb.com/developer/products/atlas/
  • w3resource MongoDB Exercises — 100+ small problems
  • HackerRank / LeetCode — search “MongoDB” tag (few but good)
  • GitHub — search “mongodb exercises” or “mongodb practice problems”
  • Build your own — take any dataset (movies, e-commerce, students, tweets) and ask yourself:
    • “How would I find top 5…?”
    • “How would I group by…?”
    • “How would I join two collections…?”

8. Teacher’s Final Advice

Do at least 3–5 exercises every day for 2–3 weeks → you will feel the difference.

Mistakes are good — break things, see errors like:

  • “path tried to be an array but found object”
  • “unknown top-level operator: $avg”
  • “exceeded memory limit”

→ Google them → understand → next time you won’t make them.

Understood beta?

Want to do Exercise Set #2 right now? (e.g. Orders + Users with $lookup + aggregation + ranking)

Or want solutions for the 12 questions above?

Or prefer different dataset (movies, e-commerce, blog posts)?

Just tell me — class is yours! 🚀❤️

Any question / doubt before we start solving? Fire away 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *