title: Mongodb aggregate with distinct
body: <p>Being completely new to Mongodb I've searched for an answer and haven't found one to this question. How can I add the option of distinct to an aggregate query?
  I have a pedigree database where I would like to find the breeders that bred the most litters in a particular year. I have a field "DOB" that in other queries I can find how many litters were bred in 2013 by "Smith" using this:</p>
  
  <pre><code>db.ped.distinct("DOB", {$and:[{"Breeder":{$regex: /Smith/}}, {"DOB-Year":2013}]}).length
  </code></pre>
  
  <p>I can figure out how many litters since multiple dogs by the same breeder with the same birthdate <em>should</em> be from the same litter.
  I would like to use this in conjunction with an aggregate but haven't been able to figure out what a query would look like. This is what I've used so far but since each document is a dog I'm getting total dogs bred by the breeder that year.</p>
  
  <pre><code>db.ped.aggregate( [
      {$match : {"DOB-Year" : 2013}},
      {$group : {_id : "$Breeder", "count" : {$sum : 1}}},
      {$sort : {"count" : -1}},
      {$limit : 15}
  ] )
  </code></pre>
  
  <p>Is there a way to use <code>$distinct</code> in the aggregate query?<br>
  Thanks.</p>
  
  <p><strong>In response to Mario</strong></p>
  
  <p>I don't think that had quite the result I wanted. I'm not sure what I'm getting exactly, but see what I got here. I've sanitized the data.</p>
  
  <pre><code>&gt; db.ped.aggregate([
  ...     {
  ...         $match: {
  ...             "DOB-Year": 2014
  ...         }
  ...     },
  ...     {
  ...         $group: {
  ...             _id: {
  ...                 "Breeder": "$Breeder",
  ...                 "DOB": "$DOB"
  ...             },
  ...             "count": {
  ...                 $sum: 1
  ...             }
  ...         }
  ...     },
  ...     {
  ...         $sort: {
  ...             "count": -1
  ...         }
  ...     },
  ...     {
  ...         $limit: 1
  ...     }
  ...  ])
  { "_id" : { "Breeder" : "Smith", "DOB" : "09/15/2014" }, "count" : 9 }
  
  &gt; db.ped.distinct("DOB", {"Breeder":{$regex: /Smith/},"DOB-Year":2014}).length
  5
  </code></pre>
  
  <p>Obviously what I really want from the first block of code should have said 5 for Smith.
  Here is an example of the data I'm using.</p>
  
  <pre><code>{
      "_id" : ObjectId("55df4a8cc0bfbb6622ae8395"),
      "PedID" : 54321,
      "RegNum" : "12345",
      "RegName" : "My Dog Name",
      "Owner" : "Smith",
      "Sex" : "Dog",
      "Color" : "Black &amp; Tan",
      "Sire" : "My Boy Bob",
      "Dam" : "My Girl Betty",
      "Breeder" : "Smith",
      "DOB" : "10/18/2014",
      "DOB-Year" : 2014
  }
  </code></pre>