mongodbschema-design

Where should I put activities timeline in mongodb, embedded in user or separately?


I am building an e-learning app, and showing student activities as a timeline, should I embed them in the user collection, or create a separate collection with an userId.

Constraints:

I have found different schemas for the related problem in these two questions:


Solution

  • You should not embed activities into student document.

    The reason I'm pretty confident of this is the following statements:

    "User activities are detailed and numerous"
    "showing student activities as a timeline"
    "teacher needs to see an summary of the activities of users"

    It is a bad practice to design schema that has ever-growing documents - so having a student document that keeps growing every time they complete/add another activity is a recipe for poor performance.

    If you want to sort student's activities, it's a lot simpler if each is a separate document in an activity collection than if it's an array within a student document.

    When you need to query about activities across multiple students, having all activities in a single collection makes it trivial, but having activities embedded in student documents makes it difficult (you will need aggregation framework, most likely, which will make it slower).

    You also say you might have need in the future to "see who finished some particular activity first? But that changes the relationship to be Many to many and is a completely different question" - this is not the case. You do not need to treat this as a many-to-many relationship - you can still store multiple activities associated with a single user and then query for all records matching activity "X" sorting by time finished (or whatever) and seeing which student has lowest time.