How can I run aggregate, min, max, sum and friends on embedded docs?
For example:
Get the average cost of ALL events that a district has, where they are pretty deeply embedded.
District.schools.all.events.all.costs.avg(:value)
Obviously doesn't work.
District.avg('schools.events.costs.value')
Neither does that. It gives this error message:
Mongo::OperationFailure: Database command 'group' failed: (errmsg: 'exception: reduce
invoke failed: JS Error: TypeError: obj.schools
has no properties reduce setup:1'; code: '9010'; ok: '0.0').
So is it possible or do I need to write my own map/reduce functions?