title: MongoDB aggregate return count of 0 if no documents
body: <p>I have a MongoDB query that groups by 5min windows based on date and returns count (which is the total number of documents in that 5min window using <code>count: { $sum: 1 }</code>). </p>
  
  <p>I'd like to have the query also return a count of 0 for a particular 5min window if no documents exist in that group. However currently, looks like only groups with a positive count are returned. </p>
  
  <p>Current Query:</p>
  
  <pre><code>        const cursor = await collection.aggregate([
              { $sort : { time : 1 } },
              { 
                  $match: {
                       $and: [ 
                          {selector: string },
                          {time: {$gte: timestamp }}
                       ]
                  }
              },
              { 
                  $group: {
                      _id: {
                          $subtract: [
                              { $subtract: [ "$time", 0 ] },
                              { $mod: [ 
                                  { $subtract: [ "$time", 0 ] },
                                  1000 * 60 * 5
                              ]}
                          ],
                      },
                      count: { $sum: 1 }
                  }
              }
          ])
  </code></pre>
  
  <p>Expected response: timestamp with count of documents including sum 0</p>
  
  <pre><code>{ _id: 1525162000000, count: 314 }
  { _id: 1523144100000, count: 0 }
  { _id: 1512155500000, count: 54 }
  </code></pre>
  
  <p>Thanks in advance!</p>