title: How to Implement $bucket to group by multiple fields
body: <p>At first <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/bucket/" rel="nofollow noreferrer">bucket</a> by age and <code>boundaries</code> is <code>[0,20,30,40,50,200]</code></p>
  
  <pre><code>db.user.aggregate(
      {$project: {_id:0, age:{$subtract:[{$year:new Date()}, {$year:"$birthDay"}]} } },
      {$bucket:{
          groupBy:"$age",
          boundaries:[0,20,30,40,50,200]
      }},
      { $project:{ _id:0,age:"$_id",count:1 } }
  )
  </code></pre>
  
  <p>got below result</p>
  
  <pre><code>{ "count" : 5, "age" : 20 }
  { "count" : 1, "age" : 30 }
  </code></pre>
  
  <p>then further I want to stat every age range count of each city</p>
  
  <pre><code>{ city : "SH", age: 20, count: 2 }
  { city : "BJ", age: 20, count: 3 }
  { city : "BJ", age: 30, count: 1 }
  </code></pre>
  
  <p>So in this case how to implement it ?</p>
  
  <p>In addition</p>
  
  <pre><code>db.user.aggregate(
      { $project: {_id:0, city:1, age:{$subtract:[{$year:new Date()}, {$year:"$birthDay"}]} } },
      { $group: { _id:"$city",ages:{$push:"$age"} } },
      { $project: {_id:0, city:"$_id",ages:1} }
  )
  
  { "city" : "SH", "ages" : [ 26, 26 ] }
  { "city" : "BJ", "ages" : [ 27, 26, 26, 36 ] }
  </code></pre>