Running sum with Mongodb

961 views
Skip to first unread message

mmcampos

unread,
Oct 13, 2010, 2:06:27 PM10/13/10
to mongodb-user
I would like to compute a running sum using mongodb. Is there a way to
do it server side using group or mapreduce?

The data I have has the following format:
city, date, count
1, 201001, 10
1, 201002, 20
2, 201001, 5
2, 201002, 40

I would like the following output

city, date, count, csum
1, 201001, 10, 10
1, 201002, 20, 30
2, 201001, 5, 5
2, 201002, 40, 45

I understand how to do it client side. But for a very large data set
it would be undesirable to send the data to the client for this
operation.

Thanks in advance for your help.

-Marcos

Tony Hannan

unread,
Oct 13, 2010, 6:30:53 PM10/13/10
to mongod...@googlegroups.com
See http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


mmcampos

unread,
Oct 14, 2010, 1:43:47 PM10/14/10
to mongodb-user
Hi Tony,

Thanks for the reply. Group returns an array of grouped items, one
element of the array per grouping key. In order to accumulate the
information across dates (for example) I would need to use city as the
grouping key. Is the idea to return an array with the values for each
of the months as the element in the array returned in group for a
given city? In other words, the result from group would be something
like this:

[{city : 1, [{date:201001, count : 10, csum : 10}, {date:201002,
count : 20, csum : 30}],
{city : 2, [{date:201001, count : 5, csum : 5}, {date:201002,
count : 40, csum : 45}]
]

Regards,

-Marcos

On Oct 13, 6:30 pm, Tony Hannan <t...@10gen.com> wrote:
> Seehttp://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsu...@googlegroups.com>
> > .

Tony Hannan

unread,
Oct 14, 2010, 2:20:03 PM10/14/10
to mongod...@googlegroups.com
It sounds like you want your group key to be city + date, i.e. {city: 1, date: 1}

To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

Tony Hannan

unread,
Oct 14, 2010, 2:22:43 PM10/14/10
to mongod...@googlegroups.com
And if you want to group by month instead of date use the key function ($keyf).

mmcampos

unread,
Oct 14, 2010, 5:12:47 PM10/14/10
to mongodb-user
I thought that if I used the key to be city + date I would not be able
to pass the information from one date to another through the prev
parameter. I thought that group would isolate the rows belonging to
each key from the ones belonging to other keys. Did I get it wrong? If
I did it would make things simpler.

Thanks again,

-Marcos

On Oct 14, 2:20 pm, Tony Hannan <t...@10gen.com> wrote:
> It sounds like you want your group key to be city + date, i.e. {city: 1,
> date: 1}
>
> > <mongodb-user%2Bunsu...@googlegroups.com<mongodb-user%252Buns...@googlegroups.com>

Tony Hannan

unread,
Oct 14, 2010, 5:52:31 PM10/14/10
to mongod...@googlegroups.com
Ok, your running sum is a little more complex than I first noticed. Running sum implies sorting dates. Your groups are definitely just by city. Then for each city you want to sort documents by date then compute running sum. So your aggregate function can accumulate the list of dates and counts, then your finalize function can sort and compute running sum.

To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages