title: mongodb aggregate distinct count
body: <p>Realise this topic has been asked many times - but the advice hasn't helped me solve this problem.</p>
  
  <p>The following query is trying to determine the presence of sales on a given weekday using ISODay.  Because the query will be run at the start of the month, I need to know how many occurrences of the specific ISOday occur in the month.</p>
  
  <pre><code>var query = { eventType: 'Sale', site : 4, tank: 1, txnDate : { "$gt" : new Date('2018-08-01T00:00:00') } };
  
  db.tankevent.aggregate([
            { $match: query },
            { $project : { 
                isoDay: { $isoDayOfWeek: "$txnDate" },
                dayDate: { $dateToString: { format: "%d", date:"$txnDate" } }
                } 
            },
            { $group: 
                { _id : { isoday: "$isoDay", dday: "$dayDate" }, count: { "$sum" : 1 } }
            },
            { $sort: { "_id.isoday": 1, "_id.dday": 1 } }
  
   ]) 
  </code></pre>
  
  <p>provides the following output</p>
  
  <pre><code>/* 1 */
  {
      "_id" : {
          "isoday" : 1,
          "dday" : "06"
      },
      "count" : 62.0
  }
  
  /* 2 */
  {
      "_id" : {
          "isoday" : 1,
          "dday" : "13"
      },
      "count" : 69.0
  }
  
  /* 3 */
  {
      "_id" : {
          "isoday" : 1,
          "dday" : "20"
      },
      "count" : 72.0
  }
  
  /* 4 */
  {
      "_id" : {
          "isoday" : 2,
          "dday" : "07"
      },
      "count" : 75.0
  }
  </code></pre>
  
  <p>I am trying to have "count" represent the number of unique "dday" records - so using the output above, I want count to be "3" for isoDay = 1.  At the moment count is reporting number of sales events that occurred for the group combination</p>