I have several hundred documents with this information:
=> User(id: integer, email: string, amount: string)
How can I sum the amount variable of all the documents?
I am using Rails + Mongoid.
thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Normally with an integer field, you could do this:
However, since your amount field is a string, it’ll just concatenate strings instead. For example, if you had 3 users with amounts of 12, 30, and 100, you would get “01230100” as a result. In this case, you may need to use something like Ruby’s inject instead: