title: Is there a way of doing calculations in mongodb and storing them
body: <p>I have a mongodb collection called cases and inside cases I have an array of cases per company object.</p>
  
  <p>So the structure is:</p>
  
  <p><a href="https://i.stack.imgur.com/rSl5x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rSl5x.png" alt="enter image description here"></a></p>
  
  <p>Is there a way in Mongo of adding things together and creating new entries. So for instance in my angular project I run through all of the cases and count all priority 1, 2, 3 etc and then run through it again and count it where the type is 1, then again and where the type is 2. </p>
  
  <p>I then save it back into mongo like this:</p>
  
  <p><a href="https://i.stack.imgur.com/SRlhZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SRlhZ.png" alt="enter image description here"></a></p>
  
  <p>** Hardware is type 1 and software is type 2.</p>
  
  <p>Basically each of those is a loop through the data and count where the value is a match and then store the final count. So in total there are 89 Open cases and of those open, there are 68 hardware and 21 software.</p>
  
  <p>I was wondering, instead of pulling all the information down and then doing loops to count the data and then returning it back to mongo, if there is a simple way of doing that in Mongo itself. So as soon as I load the data, it automatically runs through and calculates it, then stores it as variables? </p>
  
  <p>If not then thats fine, I just want to make sure I'm using the best solution to do this.</p>
  
  <p>Thank you in advance,</p>