select sum() in mongodb
mongodb, mongoid
Solution
Try this one:
Employee.where(id: 1000).sum(:salary)
Problem
Let's say I need to know how much money I've earned in whole my life: What would I do in SQL is something like this: ``` select sum(salary) from employee_salary where employee_id=1000; ``` Is there some simple way to do this in mongoDB via mongoid, rails? Don't even think to propose me using map/reduce. The easiest way I've found is: ``` db.employee_salary.group( { cond: { "employee_id":1000 }, reduce: function(obj,prev) { prev.sum += obj.salary; }, initial: { sum: 0 } } )[0].sum; ``` But it looks so awful...