I'm developing a simple financial app for keeping track of incomes and outcomes.
For the sake of simplicity, let's suppose these are some of my documents:
{ "_id" : ObjectId("54adc0659413535e02fba115"), "description" : "test1", "amount" : 100, "dateEntry" : ISODate("2015-01-07T23:00:00Z") }
{ "_id" : ObjectId("54adc21a0d150c760270f99c"), "description" : "test2", "amount" : 50, "dateEntry" : ISODate("2015-01-06T23:00:00Z") }
{ "_id" : ObjectId("54b05da766341e4802b785c0"), "description" : "test3", "amount" : 11, "dateEntry" : ISODate("2015-01-09T23:00:00Z") }
{ "_id" : ObjectId("54b05db066341e4802b785c1"), "description" : "test4", "amount" : 2, "dateEntry" : ISODate("2015-01-09T23:00:00Z") }
{ "_id" : ObjectId("54b05dbb66341e4802b785c2"), "description" : "test5", "amount" : 12, "dateEntry" : ISODate("2015-01-09T23:00:00Z") }
{ "_id" : ObjectId("54b05f4ee0933a5c02398d55"), "description" : "test6", "amount" : 4, "dateEntry" : ISODate("2015-01-09T23:00:00Z") }
What I would like now is to draw a "balance" chart, based on such data:
[
{ day:'2015-01-06', amount:50},
{ day:'2015-01-07', amount:150},
{ day:'2015-01-09', amount:179},
...
]
In other words, I need to group all my transactions by day, and for each day I need to sum all of my previous transactions ( since the beginning of the world ).
I already know how to group by day:
$group: {
_id: {
y: {$year:"$dateEntry"},
m: {$month:"$dateEntry"},
d: {$dayOfMonth:"$dateEntry"}
},
sum: ???
}
But I don't know how to go back and sum all the amounts. Imagine I need to show a monthly balance report: should I run 31 queries, one for each day summing all transaction's amount except next days? Sure I can, but don't think that's the best solution.
Thanks in advance!