title: MongoDb How to aggregate by month and year
body: <p>I am new to mongodb and trying to learn Mongodb queries</p>
  
  <pre><code>    {
      "_id" : ObjectId("59815d4704ca1760a45957ca"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 200,
      "expenseRemark" : "aa",
      "expenseCategory" : "billing",
      "entryTime" : ISODate("2017-08-02T05:03:57Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("59815d5404ca1760a45957cb"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 300,
      "expenseRemark" : "ff",
      "expenseCategory" : "transport",
      "entryTime" : ISODate("2017-08-02T05:04:11Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("5980191d04ca1760a45957cd"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 100,
      "expenseRemark" : "rr",
      "expenseCategory" : "billing",
      "entryTime" : ISODate("2017-08-01T06:00:46Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("5980192604ca1760a45957ce"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 200,
      "expenseRemark" : "qq",
      "expenseCategory" : "transport",
      "entryTime" : ISODate("2017-08-01T06:01:03Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("5980192e04ca1760a45957cf"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 470,
      "expenseRemark" : "ff",
      "expenseCategory" : "transport",
      "entryTime" : ISODate("2017-08-01T06:01:11Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("59816ac004ca1760a45957d0"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 500,
      "expenseRemark" : "raj",
      "expenseCategory" : "transport",
      "entryTime" : ISODate("2017-08-02T06:01:26Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("59816acb04ca1760a45957d1"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 100,
      "expenseRemark" : "pet",
      "expenseCategory" : "pets",
      "entryTime" : ISODate("2017-08-02T06:01:37Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("597d7a9c04ca1760a45957d2"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 500,
      "expenseRemark" : "gt",
      "expenseCategory" : "sports",
      "entryTime" : ISODate("2017-07-30T06:20:04Z"),
      "__v" : 0
  }
  {
      "_id" : ObjectId("597d7aaa04ca1760a45957d3"),
      "userEmail" : "lk@gmail.com",
      "expenseAmount" : 560,
      "expenseRemark" : "mov",
      "expenseCategory" : "entertainment",
      "entryTime" : ISODate("2017-07-30T06:20:14Z"),
      "__v" : 0
  }
  </code></pre>
  
  <p>I want to get expenseAmount grouped by <strong>Year + Month</strong>.
  I tried </p>
  
  <pre><code>`db.expenses.aggregate( 
         {$project : { 
                month : {$month : "$entryTime"}, 
                year : {$year :  "$entryTime"}
            }}, 
          {$group : { 
                  _id : {month : "$month" ,year : "$year" },  
                total : {$sum : "$expenseAmount"} 
          }})`
  </code></pre>
  
  <p>Which gives</p>
  
  <pre><code>{ "_id" : { "month" : 7, "year" : 2017 }, "total" : 0 }
  
  { "_id" : { "month" : 8, "year" : 2017 }, "total" : 0 }
  </code></pre>
  
  <p>Please guide me how can I get aggregated result.
  I  could not figure out the way to do that.</p>
  
  <p>Thanks</p>