title: MongoDB - count items using addToSet
body: <p>I'm working with a collection called "submissions" that includes mission data for a game. Every time you submit your answer to the question/mission a document is saved to the submissions collection.</p>
  
  <p>A typical document looks like this:</p>
  
  <pre><code>{ 
      "_id" : ObjectId("5b0c99dffea598002fdb6ec9"), 
      "status" : "Accepted", 
      "missionAcceptanceDate" : NumberInt(1527526261), 
      "submissionDate" : NumberInt(1527552495), 
      "location" : "", 
      "approvalDate" : null, 
      "mission_id" : ObjectId("5b0c5b10fea598002fdb6ec6"), 
      "user_id" : ObjectId("5b0c99d0fea598002fdb6ec7")
  }
  </code></pre>
  
  <p>I need to build a query that can determine the following:</p>
  
  <ol>
  <li>Counts the total submissions per grouping by status between a date range,</li>
  <li>Counts the Total number of submissions by mission_id per each grouping.</li>
  </ol>
  
  <p>Below is what I have so far ("cutting out some info..."):</p>
  
  <p><strong>QUERY</strong>:</p>
  
  <pre><code>db.submissions.aggregate(
          { $match: {
              "submissionDate": {
                  "$gte": NumberInt(1527901260), //Fri, 1 Jun 2018 18:00:01 PDT
                  "$lte": NumberInt(1530831600) //Thu, 5 Jul 2018 16:00:00 PDT
              }
            }
          },
          {$project: 
              {_id:1,
              status: 1,
              missionAcceptanceDate: 1,
              submissionDate: 1,
              approvalDate: 1,
              user_id: 1,
              mission_id: 1
              }
          },
          {$group: {
                      _id: { status: "$status" },
                      statusTotal: { $sum: 1 },
                      mission_id: { $addToSet: "$mission_id" },
                      }
           }
  
  )
  </code></pre>
  
  <p><strong>RESULT</strong>:</p>
  
  <pre><code>{ 
      "_id" : {
          "status" : "Rejected"
      }, 
      "statusTotal" : 16.0, 
      "mission_id" : [
          ObjectId("5b0edbfafea598002fdb6f3a"), 
          ObjectId("5b0f0131fea598002fdb6f43"), 
          ObjectId("5b0eded6fea598002fdb6f3d"), 
          ...
      ]
  }
  { 
      "_id" : {
          "status" : "Approved"
      }, 
      "statusTotal" : 592.0, 
      "mission_id" : [
          ObjectId("5b391cf4e177c700308dd0ef"), 
          ObjectId("5b36a0d172b5240030d304be"), 
          ObjectId("5b3558276a0f950030db1732"), 
          ...
      ]
  }
  </code></pre>
  
  <p>This is a great, but I also need to know the Total Number of Times each "mission_id" was "Rejected" or "Approved". The desired out come should look something like the following:</p>
  
  <pre><code>{ 
      "_id" : {
          "status" : "Rejected"
      }, 
      "statusTotal" : 16.0, 
      "mission_id" : [
  
          ...
          { mission_id: ObjectId("5b0edbfafea598002fdb6f3a"), count: 3 },
          { mission_id: ObjectId("5b0f0131fea598002fdb6f43"), count: 5},
          { mission_id: ObjectId("5b0eded6fea598002fdb6f3d"), count: 2 } 
          ...
      ]
  }
  { 
      "_id" : {
          "status" : "Approved"
      }, 
      "statusTotal" : 592.0, 
      "missionData" : [ 
          { mission_id: ObjectId("5b391cf4e177c700308dd0ef"), count: 23 },
          { mission_id: ObjectId("5b36a0d172b5240030d304be"), count: 45},
          { mission_id: ObjectId("5b3558276a0f950030db1732"), count: 15 } 
          ...
      ]
  }
  </code></pre>
  
  <p>I tried using $size, $sum, $count, even using a second $group with {$sum:1} for each mission_id, but just can't figure it out. Any help with this would be so much appreciated. Thank you in advance!</p>