title: Group and count by month
body: <p>I have a booking table and I want to get number of bookings in a month i.e. group by month.</p>
  <p>And I am confused that how to get month from a date.</p>
  <p>Here is my schema:</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;5485dd6af4708669af35ffe6&quot;),
      &quot;bookingid&quot; : 1,
      &quot;operatorid&quot; : 1,
      ...,
      &quot;bookingdatetime&quot; : &quot;2012-10-11T07:00:00Z&quot;
  }
  {
      &quot;_id&quot; : ObjectId(&quot;5485dd6af4708669af35ffe7&quot;),
      &quot;bookingid&quot; : 2,
      &quot;operatorid&quot; : 1,
      ...,
      &quot;bookingdatetime&quot; : &quot;2014-07-26T05:00:00Z&quot;
  }
  {
      &quot;_id&quot; : ObjectId(&quot;5485dd6af4708669af35ffe8&quot;),
      &quot;bookingid&quot; : 3,
      &quot;operatorid&quot; : 2,
      ...,
      &quot;bookingdatetime&quot; : &quot;2014-03-17T11:00:00Z&quot;
  }
  </code></pre>
  <p>And this is I have tried:</p>
  <pre><code>db.booking.aggregate([
    { $group: {
      _id: new Date(&quot;$bookingdatetime&quot;).getMonth(),
      numberofbookings: { $sum: 1 }
    }}
  ])
  </code></pre>
  <p>but it returns:</p>
  <pre><code>{ &quot;_id&quot; : NaN, &quot;numberofbookings&quot; : 3 }
  </code></pre>
  <p>Where am I going wrong?</p>