I’m building a system that involves users and teachers. In this particular system however I would like to categorize the teachers, but the tricky part is the categories are dynamic thus they can change anytime.
I have to have some functions, since I’m developing the backend;
- The first one is showAllCategories(), that shows all the main categories.
- Second is the showSubcategories() which shows the subcategories of a category()
- Third is the showContent(), which in this case shows the teacher’s information.
Before asking mighty Stack-Overflowers how would this be efficiently implemented, I thought I could use a doubly linked list approach where in categories table CategoryName, Before, After, Content and if the category did not have the after, the content would be pointing to the teacher’s table. This is my classic SQL approach however I’m using MongoDB and since I’m a beginner I wonder if I could take the advantage of NoSQL in this particular situation?
MongoDb natively supports the Array type, which behaves actually more like a list. With
$pushand$pullyou can add and remove elements from such an array field.$addToSeteven makes sure there are no dublicates.Now is the question of how the categories are stored. You can make a collection
categorieswith the main categories, and they would be having a field each that has the array of the sub-categories:Your
teachercollection on the other hand would then have an array of embedded documents, the categories of the teacher. They are duplicates of those found in thecategoriescollection, minus the fields that you won’t need in the teacher view. This way you avoid joins, since they don’t exist in MongoDB.The rest I am sure you can think up.
Addition: In short, use the flexible types that MongoDB offers, and don’t worry about data redundancy. Embed documents often and don’t forget the indexes.