Search code examples
node.jsmongodbmongoosemongoose-schema

Summing the values in mongoose


I have a mongoose Schema which keeps track of 5 types of accidents every year . Now I also want a "Total Accidents" column which should simply sum up the values of those 5 columns and gets updated every time any one of the 5 columns gets update . Can someone please tell me how to do it ?


Solution

  • Well the only way to do it, manually update it each time you do something with these 5 fields (we don't have columns in MongoDB). :)

    If you are using tools like Mongoose, you could add some pre save hook that will do that magic.