title: Aggregate total and unique counts based on value type and unique visitorId - MongoDB
body: <p>Similar to another question I had (<a href="https://stackoverflow.com/q/71068814/16957532">Here</a>). But now I'm trying to count unique and total events on daily basis for each event type, based on the following data shape:</p>
  <pre><code>{
      username: &quot;jack&quot;,
      events: [
       {
         eventType: &quot;party&quot;,
         createdAt: &quot;2022-01-23T10:26:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;87654321-0ebb-4238-8bf7-87654321&quot;
             }
       },
       {
         eventType: &quot;party&quot;,
         createdAt: &quot;2022-01-23T10:26:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;87654321-0ebb-4238-8bf7-87654321&quot;
             }
       },
       {
         eventType: &quot;party&quot;,
         createdAt: &quot;2022-01-23T10:26:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;01234567-0ebb-4238-8bf7-01234567&quot;
             }
       },
      {
         eventType: &quot;meeting&quot;,
         createdAt: &quot;2022-01-23T12:26:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;87654321-0ebb-4238-8bf7-87654321&quot;
             }
       },
       {
         eventType: &quot;meeting&quot;,
         createdAt: &quot;2022-01-23T11:26:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;87654321-0ebb-4238-8bf7-87654321&quot;
             }
       },
       {
         eventType: &quot;meeting&quot;,
         createdAt: &quot;2022-01-23T12:26:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;01234567-0ebb-4238-8bf7-01234567&quot;
             }
       },
       {
         eventType: &quot;party&quot;,
         createdAt: &quot;2022-01-30T10:26:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;12345678-0ebb-4238-8bf7-12345678&quot;
             }
       },
      {
         eventType: &quot;party&quot;,
         createdAt: &quot;2022-01-30T10:16:11.214Z&quot;,
         visitorInfo: {
              visitorId: &quot;12345678-0ebb-4238-8bf7-12345678&quot;
             }
       },
      {
         eventType: &quot;meeting&quot;,
         createdAt: &quot;2022-01-30T12:36:11.224Z&quot;,
         visitorInfo: {
              visitorId: &quot;12345678-0ebb-4238-8bf7-12345678&quot;
             }
       },
      {
         eventType: &quot;meeting&quot;,
         createdAt: &quot;2022-01-30T11:46:11.314Z&quot;,
         visitorInfo: {
              visitorId: &quot;12345678-0ebb-4238-8bf7-12345678&quot;
             }
       }
         ]
  
      }
  </code></pre>
  <p>I'm trying to count events (all and unique ones based on visitorId) on date (daily).
  This is what I have so far (thanks to @R2D2's guide on the approach):</p>
  <pre><code> Event.aggregate([
    { $match: { username: 'jack' } },
  
    { $unwind: &quot;$events&quot; },
  
    {
      $project: {
        totalPartyEvents: {
          $cond: [
            {
              $eq: [&quot;$events.eventType&quot;, &quot;party&quot;],
            },
            1,
            0,
          ],
        },
  
      uniquePartyEvents: { // where I'm stuck. I need to count unique events based on visitorId on current date for 'party' event type.
          $cond: [
            {
              $eq: [&quot;$events.eventType&quot;, &quot;party&quot;],
            },
            1,
            0,
          ],
        },
  
        totalMeetingEvents: {
          $cond: [
            {
              $eq: [&quot;$events.eventType&quot;, &quot;meeting&quot;],
            },
            1,
            0,
          ],
        },
  
      uniqueMeetingEvents: { // do the same for other events. maybe there's a better way to combine these (with facets).
          $cond: [
            {
              $eq: [&quot;$events.eventType&quot;, &quot;meeting&quot;],
            },
            1,
            0,
          ],
        },
  
        date: &quot;$events.createdAt&quot;,
      },
    },
  
    {
      $group: {
        _id: {
          $dateToString: { format: &quot;%Y-%m-%d&quot;, date: &quot;$date&quot; },
        },
  
        totalPartyEvents: {
          $sum: &quot;$totalMeetingEvents&quot;,
        },
        uniquePartyEvents: {
          $sum: &quot;$totalMeetingEvents&quot;,
        },
      
        totalMeetingEvents: {
          $sum: &quot;$totalMeetingEvents&quot;,
        },
  
        uniqueMeetingEvents: {
          $sum: &quot;$uniqueMeetingEvents&quot;,
        },
      },
    },
    {
      $project: {
        date: &quot;$_id&quot;,
        uniquePartyEvents: 1,
        totalPartyEvents: 1,
        totalMeetingEvents:1,
        uniqueMeetingEvents: 1,
  
      },
    },
    {
      $group: {
        _id: &quot;0&quot;,
        dateAndEventFrequency: {
          $push: &quot;$$ROOT&quot;,
        },
      },
    },
    {
      $project: {
        _id: 0,
        dateAndEventFrequency: 1,
      },
    },
  ]);
  </code></pre>
  <p>I tried using <code>$addToSet</code> but it's not used with <code>$project</code> (it works with <code>$group</code>).
  Any new approach is welcome based on the data shape and the desired result I'm expecting. I used <code>$project</code> because I was already using it.</p>
  <p>Basically what I'm hoping to get in the end:</p>
  <pre><code>dateAndEventFrequency: [
      {
      _id: &quot;2022-01-23&quot;,
      totalPartyEvents: 3,
      uniquePartyEvents: 2,
      totalMeetingEvents: 3,
      uniqueMeetingEvents: 2,
      date: &quot;2022-01-23&quot;,
      },
     {
      _id: &quot;2022-01-30&quot;,
      totalPartyEvents: 2,
      uniquePartyEvents: 1,
      totalMeetingEvents: 2,
      uniqueMeetingEvents: 1,
      date: &quot;2022-01-30&quot;,
      },
  ]
  </code></pre>
  <p>I'm using Mongoose and Nodejs. Any help or guidance is appreciated. Thanks!</p>