Say you have a Person
collection with models that look this:
{
name: "Bob",
age: 24,
gender: "Male",
start_date: ISODate("2014-01-30T06:01:17.171Z")
salary: 80000
}
You want to store some calculated fields - some of them are at the document level - e.g. days_in_service
might be the number of days since start_date (maybe not the best example, since it changes, but you get the idea) , or monthly_salary
might be salary
dvided by 12.
Some of them are at the collection level - e.g. percentage_males
is the percentage of people that are male, or average_salary
is the average salary across the collection.
What is the best way of handling these things in Meteor?
Coming from Django, there are a few ways to handle computed fields.
For instance-level things, you can make them model properties (using @), so that you can access them as a normal attribute of a model, but they’ll be calculated each time you access them.
Or you can store them as actual fields along with each model, and use signals to keep them updated.
What would be the equivalent to these in Meteor?
For example, how would you best create a dynamic field that was calculated each time?
Or if you wanted to actually store it as a new field - how would you create it so that it was reactive, like other fields in Meteor? So that for example, if you updated a person’s salary, their monthly_salary field would also automatically be updated?
Or if it was a collection-level thing and you had the averages stored in another collection - if you added a new person, how would you ensure the averages got updated?