I would like to store some info of a model in a model that the first model references.
Consider i have these classes (ids omitted)
class Student:
currentClassroom = ForeignKey(Classroom)
age = Integer
class Classroom:
totalAgeOfStudents = Integer
So, if I add a student to a classroom, it's classroom should also be updated. In more complex cases, consider we also have Campus, City, and Province. So, when a student is added/updated, it's new and old classroom, classroom's campus, campus' city and city's province should also be updated. These models are fetched so often that calculating per-request is out of question here.
Before I migrated to node.js, i used to do something like this in django (with MySQL): Every model has a 'calculate' method. whenever a student is updated, it's old and new classrooms are flagged as dirty in this calculate method. After that, models flagged as dirty are processed, their calculate method is called, they set their fields accordingly (for example, they calculate total age of students by querying the student table), then if some other model should also change, they flag it dirty too. And so on.
I don't think this method is nice at all. I tried save/delete/update hooks in django (mongoose also have save/delete hooks), but the situation doesn't change: whenever I receive an event of a student, I first update the fields of his/her classroom, then trigger classroom's corresponding event and so on.
Is there a better way for this in mongoose that is more effective and nicer?
Bounty Edit: I know that in my example, I can flag classroom as dirty and then use aggregation methods of MongoDB to calculate related fields. I'm asking for a nicer approach.