0

I have a mongodb:

    {
    "_id" : ObjectId("59816ac004ca1760a45957d0"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 500,
    "expenseRemark" : "raj",
    "expenseCategory" : "transport",
    "transactionType":"debit",
    "entryTime" : ISODate("2017-08-02T06:01:26Z"),
    "__v" : 0
},
{
    "_id" : ObjectId("59816acb04ca1760a45957d1"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 100,
    "expenseRemark" : "pet",
    "expenseCategory" : "pets",
    "transactionType":"debit",
    "entryTime" : ISODate("2017-08-02T06:01:37Z"),
    "__v" : 0
},
{
    "_id" : ObjectId("597d7a9c04ca1760a45957d2"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 500,
    "expenseRemark" : "gt",
    "expenseCategory" : "sports",
    "transactionType":"debit",
    "entryTime" : ISODate("2017-07-30T06:20:04Z"),
    "__v" : 0
},
{
    "_id" : ObjectId("597d7aaa04ca1760a45957d3"),
    "userEmail" : "lk@gmail.com",
    "expenseAmount" : 560,
    "expenseRemark" : "mov",
    "expenseCategory" : "entertainment",
    "transactionType":"debit",
    "entryTime" : ISODate("2017-07-30T06:20:14Z"),
    "__v" : 0
}

I want the result that give sum of expenseAmount for the month of July. How can I achieve that, so far I have tried,

`

db.expenses.aggregate(
  [ 
    { 
      $match:{"userEmail":"lk@gmail.com","entryTime":{"$month": 7}}
    }, 
    {$group:{ _id : null, total : {$sum : "$expenseAmount"}}} 
  ]
)

`

But it is not working. Please guide me , how can I do that. I trie various things but none work. Though I can get the result by specifying $lt & $gt in entryTime

Rgds

raju
  • 6,448
  • 24
  • 80
  • 163
  • `$month` is not for use in `$match`. Instead you use a "range" just like you did in your previous question. The `$month` would be used in the `$group` in order to form the `_id` grouping key. – Neil Lunn Aug 04 '17 at 08:43
  • Any clue , how can i do that query by just month number? – raju Aug 04 '17 at 08:46

0 Answers0