I am trying to implement a collection in meteor/mongo which is of following nature:

FIRST_NAME-------LAST_NAME-------------CLASSES----------PROFESSORS


----------A-----------------------B------------------------------a---------------------b

-------------------------------------------------------------------c---------------------d

-------------------------------------------------------------------e---------------------f

-------------------------------------------------------------------g---------------------h


-------------M-------------------------N------------------------c---------------------d

-------------------------------------------------------------------p---------------------q

-------------------------------------------------------------------x---------------------q

-------------------------------------------------------------------m---------------------n

-------------------------------------------------------------------r---------------------d

So as above, a person can take multiple classes and a class can have multiple people. Now, I want to make this collection searchable and sortable by all possible fields. (Also that one professor can teach multiple classes.)

Searching by FIRST_NAME and LAST_NAME is easy in above shown model. But, I should be able to see all student depending on the class I select. I would also want to see list of classes sorted in alphabetical order and also the people enrolled in corresponding classes?

Can you please let me know how to approach this in a meteor/mongo style? I would also be glad if you could lead me to any resources available on this?

1

There are 1 answers

3
Jesper We On BEST ANSWER

You are describing one of the typical data structures which are better suited for a relational database. But don't worry. For reasonably sized data sets it is quite workable in MongoDB too.

When modelling this type of structure in a document database you use embedding, which does lead to data duplication, but this data duplication is typically not a problem.

Pseudo-code for your model:

Collection schoolClass: { // Avoid the reserved word "class"
    _id: string,
    name: string,
    students: [ { _id: string, firstName: string, lastName: string } ],
    professor: { _id: string, firstName: string, lastName: string }
}

Collection student: {
    _id: string,
    firstName: string, 
    lastName: string,
    classes: [ { _id: string, name: string } ]
}

Collection professor: {
    _id: string,
    firstName: string, 
    lastName: string,
    classes: [ { _id: string, name: string } ]
}

This gives you easily searchable/sortable entry points to all objects. You only follow the "relation" _id to the next collection if you need some special data from an object. All data needed for all documents in the common queries should be present in the Collection the query is run on.

You just need to make sure you update all the relevant collections when an object changes.

A good read is https://docs.mongodb.com/manual/core/data-modeling-introduction/