These are chat archives for Automattic/mongoose

Aug 2018
Maurizio Bellemo
Aug 26 2018 08:51
Hi all, I am trying to understand which is the best way to save aggregate values if you also want to sort using them. Right now, I have a customer object with inside an array of deals.... The deals have 3 statuses (lost, won, pipeline) and a monetary value. What I would like to do is to create 3 sums (one per status) and use them in the customer...
What is the best way to go?
  1. Virtual (but I guess i cannot sort by it then which is bad)
  2. Add a hook for the customer object pre save (and recalculate the sums whenever the customer object is updated).
  3. A better way?
    On top of if, shall i use $aggregate and how?
    thanks a lot