Best way to keep track of bought digital content of users

Hey there :grin: I am working on a schema design where some users create digital courses and others can buy them - think of it like Udemy. I am having trouble deciding on the best way of storing the bought courses so the fetch queries are efficient.

  1. Option - store references of courses a user buys with a date in a boughtCourses array inside the user document. The array in the User model would look like this:
boughtCourses: [ { enrolledAt: Date, course: ReferenceID }, { enrolledAt: Date, course: ReferenceID } ]

This approach is straightforward but can lead to an unbound array issue (really small chance tho). I would use populate from mongoose to get the referenced data. Implementing pagination with filtering and also providing the total number of documents may not be that efficient for a large number of documents.

  1. Option - I would prepare a separate collection named CourseEnrollment which would hold the enrollment information for all courses. Each document would look something like this:
course: CourseID;
buyer: BuyerID;
seller: SellerID;
createdAt: Date;

To fetch the courses of a user I would create an index on the buyer field and fetch them by it. However I am not sure about the large number of documents over time that would appear in the collection as every buy would insert a new document inside. Fetching the bought courses may be easier however the size of the collection may become to much to include it in the cache.

Which option would you say is better?

Hey @Jan_Ivanovic,

Welcome to the MongoDB Community Forums! :leaves:

You have correctly mentioned both the advantages and disadvantages of the two options.

Yes, even though the chance is small, it’s still there. And when this happens, complex workaround would be required. It’s best to design the schema so that this has no possibility of happening.
A general thumb rule to follow while schema designing in MongoDB is things that are queried together should stay together. Thus, it may be beneficial to work from the required queries first and let the schema design follow the query pattern.

I would recommend you experiment with multiple schema design ideas. You can use mgeneratejs to create sample documents quickly in any number, so the design can be tested easily.

Please let us know if you have any additional questions. Feel free to reach out for anything else as well.


1 Like

Hey! Thank you for a quick and great response Satyam :rocket:

I have decided to go with the second option for now, since it is more scale prone. The schema design cases depend on the domain you are working on and sometimes it is hard to choose a solution because there are no right or wrong answers. If needed I will adjust the designs later on but I think this option should serve me well. Also, thanks for linking mgeneratejs!

Have a great day :wave:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.