我目前正在学习mongodb查询。我的收藏费用就像::
{
"_id" : ObjectId("59815d4704ca1760a45957ca"),
"userEmail" : "lk@gmail.com",
"expenseAmount" : -200,
"expenseRemark" : "aa",
"expenseCategory" : "billing",
"transactionType" : "debit",
"entryTime" : ISODate("2017-08-02T05:03:57Z"),
"__v" : 0
}
{
"_id" : ObjectId("59815d5404ca1760a45957cb"),
"userEmail" : "lk@gmail.com",
"expenseAmount" : -300,
"expenseRemark" : "ff",
"expenseCategory" : "transport",
"transactionType" : "debit",
"entryTime" : ISODate("2017-08-02T05:04:11Z"),
"__v" : 0
}
{
"_id" : ObjectId("5980191d04ca1760a45957cd"),
"userEmail" : "lk@gmail.com",
"expenseAmount" : -100,
"expenseRemark" : "rr",
"expenseCategory" : "billing",
"transactionType" : "debit",
"entryTime" : ISODate("2017-08-01T06:00:46Z"),
"__v" : 0
}
{
"_id" : ObjectId("5980192604ca1760a45957ce"),
"userEmail" : "lk@gmail.com",
"expenseAmount" : -200,
"expenseRemark" : "qq",
"expenseCategory" : "transport",
"transactionType" : "debit",
"entryTime" : ISODate("2017-08-01T06:01:03Z"),
"__v" : 0
}
{
"_id" : ObjectId("5980192e04ca1760a45957cf"),
"userEmail" : "lk@gmail.com",
"expenseAmount" : -470,
"expenseRemark" : "ff",
"expenseCategory" : "transport",
"transactionType" : "debit",
"entryTime" : ISODate("2017-08-01T06:01:11Z"),
"__v" : 0
}
{
"_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
}
{
"_id" : ObjectId("5983f28723485dd901791bc9"),
"userEmail" : "lk@gmail.com",
"expenseAmount" : 5560,
"expenseRemark" : "income",
"expenseCategory" : "entertainment",
"transactionType" : "credit",
"entryTime" : ISODate("2017-07-25T06:20:14Z"),
"__v" : 0
}
我想要一个结果,我可以得到 expenseAmount 的总数,这是两个日期之间 entryTime 。
我正在尝试
db.expenses.aggregate(
{$match:{"entryTime":{$gt: new ISODate("2017-07-01T00:00:00Z") , $lt: new ISODate("2017-07-31T00:00:00Z")}}},
{
$group:{,
_id : {total : {$sum : "$expenseAmount"}}
}
}
)
我尝试了几个不同的东西,但无法得到结果。 但这不起作用。任何人都可以帮助我获得两个日期之间的费用总额。
EDIT-1
db.expenses.aggregate(
{$match:{"entryTime":{$gt: new ISODate("2017-07-01T00:00:00Z") , $lt: new ISODate("2017-07-31T00:00:00Z")}}},
{"$group": {"_id": null, "total": { "$sum": "$expenseAmount"}}}
);