Question 28 / 50
ImportantMediumScenarioData Modeling
Users can enrol in many courses, and courses have many users. How would you model that in MongoDB?
Think about it first.
Short answer
Usually two collections plus an enrolment collection, or an array of courseIds on the user if the list stays small. Do not embed full user documents inside courses or full courses inside users — both sides can grow and are queried independently.
Why?
If 'Rahul's courses' is the hot read and he has dozens, not thousands, of courseIds, an array on the user is fine. If you need enrolment dates, progress, and completion, that is its own document. $lookup or a second query loads course titles when needed.
Interview tip
Reject both 'always embed' and 'always a join table like SQL' until you hear the access pattern.
Common mistake
An unbounded students[] array on the course document for a popular course.
How did you do?
Cheat Sheet