title: MongoDB aggregation framework query for sum time differences
body: <p>we have events like these:</p>
  
  <pre><code>{
    "dateStart": "2018-04-04T08:50:42.389Z",
    "machine": "Machine1",
    "state": "Running"
  },
  {
    "dateStart": "2018-04-04T09:10:00.389Z",
    "machine": "Machine1",
    "state": "Pause"
  },
  {
    "dateStart": "2018-04-04T09:11:00.000Z",
    "machine": "Machine1",
    "state": "Running"
  },
  </code></pre>
  
  <p>What is the MongoDB query to calculate the amount of time that Machine1 was in state Running in a specific day/week/month?</p>
  
  <p>is that even possible?</p>
  
  <p>since we are in the design phase, we are open to suggestion on modification of the JSON schema. What is important here, is the ability to calculate sum of duration from the events.</p>