mongoid/mongoid — pull request #1473 by spiegela (2011-12-04 03:15:08+00:00)
Title: New context method:  reduce

All,

Please review these commits. I've added a new method to contexts/mongo so that you can both group, and then apply consolidation methods (aggregate, sum, min or max) to one or more fields.

For instance:

```ruby
Person.only(:city).reduce(:weight => :sum)
```

would give you:

```ruby
[
{"city" => "Kansas City", "weight_sum" => "123456"},
{"city" => "St Louis", "weight_sum" => "234567"}
]
```

where "123456" is the weight of all the people in my home town, and "234567" is the weight of all the people in St Louis (our Missouri Rival)

There's also support to use your own Javascript like so:

```ruby
Person.only(:city).reduce(
:weight => "function(prev, obj){ }"
)
```

Let me know what you think!