我有一个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
}
我希望结果能够给出7月份expenseAmount
的总和。我怎么能实现这一点,到目前为止我已经尝试过了,
`
db.expenses.aggregate(
[
{
$match:{"userEmail":"lk@gmail.com","entryTime":{"$month": 7}}
},
{$group:{ _id : null, total : {$sum : "$expenseAmount"}}}
]
)
`
但它不起作用。请指导我,我该怎么做。我做了各种各样的事情但没有工作。虽然我可以通过在entryTime中指定$lt & $gt
来获得结果
RGDS