title: Group count with MongoDB using aggregation framework
body: <p>Let's say my MongoDB schema looks like this:</p>
  
  <pre><code>{car_id: "...", owner_id: "..."}
  </code></pre>
  
  <p>This is a many-to-many relationship.  For example, the data might look like this:</p>
  
  <pre><code>+-----+----------+--------+
  | _id | owner_id | car_id |
  +-----+----------+--------+
  |   1 |        1 |      1 |
  |   2 |        1 |      2 |
  |   3 |        1 |      3 |
  |   4 |        2 |      1 |
  |   5 |        2 |      2 |
  |   6 |        3 |      4 |
  |   7 |        3 |      5 |
  |   8 |        3 |      6 |
  |   9 |        3 |      7 |
  |  10 |        1 |      1 | &lt;-- not unique
  +-----+----------+--------+
  </code></pre>
  
  <p>I want to get the number of cars owned by each owner.  In SQL, this might look like:</p>
  
  <pre><code>SELECT owner_id, COUNT(*) AS cars_owned
  FROM (SELECT owner_id FROM car_owners GROUP BY owner_id, car_id) AS t
  GROUP BY owner_id;
  </code></pre>
  
  <p>In this case, the result would look like this:</p>
  
  <pre><code>+----------+------------+
  | owner_id | cars_owned |
  +----------+------------+
  |        1 |          3 |
  |        2 |          2 |
  |        3 |          4 |
  +----------+------------+
  </code></pre>
  
  <p>How can I accomplish this same thing using MongoDB using the aggregation framework?</p>