title: How to use MongoDB $group stage to both group and count repeated values?
body: <p>I am having trouble with the $group stage in my aggregation. I want to group all the "recentPlays.quiz" values together and count the repeated values, so the end result I want from the aggregation is two fields: the quiz object and the total. In this case it would be something like:</p>
  
  <pre><code>{
          "recentPlays" : [
              {
                  "quiz" : {
                      "author" : "red-tester1",
                      "title" : "Asdffff Dfasdf"
                  },
                  "count": 1
              },
              {
                  "quiz" : {
                      "author" : "red-tester3",
                      "title" : "Creation Test 2"
                  },
                  "count": 1
              },
              {
                  "quiz" : {
                      "author" : "blue-tester1",
                      "title" : "Finky Fink"
                  },
                  "count": 4
              }
          ]
  }
  </code></pre>
  
  <p>Here is the aggregation I have so far:</p>
  
  <pre><code>db.users.aggregate([
      {$match: { "recentPlays.date": {$gte:twentyFourHrsAgo}}},
      {$project: {"recentPlays.quiz":1, _id:0}}
  ]).pretty();
  </code></pre>
  
  <p>Here is that aggregation's output:</p>
  
  <pre><code>MongoDB shell version: 3.2.1
  connecting to: videoQuiz
  {
      "recentPlays" : [
          {
              "quiz" : {
                  "author" : "red-tester1",
                  "title" : "Asdffff Dfasdf"
              }
          },
          {
              "quiz" : {
                  "author" : "red-tester3",
                  "title" : "Creation Test 2"
              }
          },
          {
              "quiz" : {
                  "author" : "blue-tester1",
                  "title" : "Finky Fink"
              }
          },
          {
              "quiz" : {
                  "author" : "blue-tester1",
                  "title" : "Finky Fink"
              }
          },
          {
              "quiz" : {
                  "author" : "blue-tester1",
                  "title" : "Finky Fink"
              }
          },
          {
              "quiz" : {
                  "author" : "blue-tester1",
                  "title" : "Finky Fink"
              }
          }
      ]
  }
  </code></pre>
  
  <p>Here is the entire collection:</p>
  
  <pre><code>MongoDB shell version: 3.2.1
  connecting to: videoQuiz
  {
      "_id" : ObjectId("580f7be62c6fd3c8065577f5"),
      "user" : "blue-tester1",
      "email" : "aslfjjcc@lkcjasdc.com",
      "createdAt" : ISODate("2016-10-25T15:36:06.933Z"),
      "recentPlays" : [
          {
              "quiz" : {
                  "author" : "red-tester1",
                  "title" : "Asdffff Dfasdf"
              },
              "score" : "0",
              "date" : ISODate("2016-10-25T15:36:27.546Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester3",
                  "title" : "Creation Test 2"
              },
              "score" : "100",
              "date" : ISODate("2016-10-25T15:37:09.142Z")
          }
      ],
      "mostRecentQuiz" : {
          "author" : "red-tester3",
          "title" : "Creation Test 2"
      },
      "mostRecentQuizTime" : ISODate("2016-10-25T15:37:09.142Z"),
      "plays" : 2
  }
  {
      "_id" : ObjectId("580a5dea650296d808082e65"),
      "user" : "red-tester3",
      "email" : "aldkdk@ccc.com",
      "createdAt" : ISODate("2016-10-21T18:26:50.870Z"),
      "recentPlays" : [
          {
              "quiz" : {
                  "author" : "red-tester2",
                  "title" : "TOP PLAYED QUIZ - Today"
              },
              "score" : "0",
              "date" : ISODate("2016-10-21T18:27:16.292Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester2",
                  "title" : "TOP LIKED QUIZ - TODAY"
              },
              "score" : "100",
              "date" : ISODate("2016-10-21T18:27:32.788Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester2",
                  "title" : "TOP LIKED QUIZ - TODAY"
              },
              "score" : "100",
              "date" : ISODate("2016-10-21T18:27:44.497Z")
          },
          {
              "quiz" : {
                  "author" : "Bertram",
                  "title" : "frfrf"
              },
              "score" : "100",
              "date" : ISODate("2016-10-21T18:28:43.893Z")
          },
          {
              "quiz" : {
                  "author" : "Bertram",
                  "title" : "Here We Go With the New Thing"
              },
              "score" : "0",
              "date" : ISODate("2016-10-21T18:43:51.205Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester3",
                  "title" : "Presidents of the United States"
              },
              "score" : "0",
              "date" : ISODate("2016-10-23T00:53:29.167Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester3",
                  "title" : "Presidents of the United States"
              },
              "score" : "0",
              "date" : ISODate("2016-10-23T00:53:44.815Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester3",
                  "title" : "Creation Test 1"
              },
              "score" : "100",
              "date" : ISODate("2016-10-23T23:50:55.355Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester3",
                  "title" : "Creation Test 2"
              },
              "score" : "100",
              "date" : ISODate("2016-10-23T23:52:33.210Z")
          },
          {
              "quiz" : {
                  "author" : "red-tester3",
                  "title" : "Here Is a New Title"
              },
              "score" : "100",
              "date" : ISODate("2016-10-23T23:58:53.683Z")
          }
      ],
      "mostRecentQuiz" : {
          "author" : "red-tester3",
          "title" : "Here Is a New Title"
      },
      "mostRecentQuizTime" : ISODate("2016-10-23T23:58:53.683Z"),
      "plays" : 10,
      "likedQuizzes" : [
          {
              "title" : "TOP LIKED QUIZ - TODAY",
              "author" : "red-tester2",
              "date" : ISODate("2016-10-21T18:27:34.893Z")
          },
          {
              "title" : "frfrf",
              "author" : "Bertram",
              "date" : ISODate("2016-10-21T18:28:45.863Z")
          },
          {
              "title" : "Here We Go With the New Thing",
              "author" : "Bertram",
              "date" : ISODate("2016-10-21T18:43:53.148Z")
          }
      ],
      "createdQuizzes" : [
          {
              "title" : "Yeah Here We Go",
              "id" : ObjectId("580a63f274b9a89c061f973e")
          },
          {
              "title" : "Z Alpha",
              "id" : ObjectId("580a641474b9a89c061f973f")
          },
          {
              "title" : "Tags Limit Test",
              "id" : ObjectId("580a6bda8d8049ac0bc1df2e")
          },
          {
              "title" : "Tags Limit test2",
              "id" : ObjectId("580a6bf98d8049ac0bc1df2f")
          },
          {
              "title" : "Presidents of the United States",
              "id" : ObjectId("580c09d28d8049ac0bc1df30")
          },
          {
              "title" : "Creation Test 1",
              "id" : ObjectId("580d4cca8d8049ac0bc1df31")
          },
          {
              "title" : "Creation Test 2",
              "id" : ObjectId("580d4d2d8d8049ac0bc1df32")
          },
          {
              "title" : "Here Is a New Title",
              "id" : ObjectId("580d4ead8d8049ac0bc1df33")
          }
      ]
  }
  </code></pre>
  
  <p>Thanks in advance for any guidance. Please excuse the dummy text in these documents, it is for testing purposes only.</p>