Mongo Mapper equivalant of ActiveRecord's sum

110 views
Skip to first unread message

Eric Anderson

unread,
Apr 11, 2013, 7:43:38 PM4/11/13
to mongo...@googlegroups.com
What is the equivalent to ActiveRecord's sum method? So:

User.sum 'credit'

Bonus points if I could use the more advanced calculate options like I can on AR:

User.sum 'credit', group: 'role', limit: 100

Do I need to do some sort of map/reduce function here. Or is there something pre-built I can use?

Thanks!

Eric

Joseph Caudle

unread,
Apr 11, 2013, 7:57:34 PM4/11/13
to mongo...@googlegroups.com
I believe this should work if you were to call `User.all.sum 'credit'`. I'm not certain about the group or limit arguments.

Best,
Joseph

--
--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Eric Anderson

unread,
Apr 11, 2013, 7:59:52 PM4/11/13
to mongo...@googlegroups.com
Isn't that pulling all the records and calculating it in Ruby? The equivalent AR code executes on the database server for better performance.

Joseph Caudle

unread,
Apr 11, 2013, 8:04:15 PM4/11/13
to mongo...@googlegroups.com
Yeah, the code I mentioned is doing the calculation in Ruby, not on Mongo. I'm not certain if Mongo is capable of that sort of calculation in the database.

Best,
Joseph

Eric Anderson

unread,
Apr 11, 2013, 8:07:05 PM4/11/13
to mongo...@googlegroups.com
On Thursday, April 11, 2013 8:04:15 PM UTC-4, Joseph Caudle wrote:
Yeah, the code I mentioned is doing the calculation in Ruby, not on Mongo. I'm not certain if Mongo is capable of that sort of calculation in the database.

$sum looks promising but the docs leave a bit to be desired so I'm not sure if it really does what I want or how to use it.

Eric 

Jamie Orchard-Hays

unread,
Apr 12, 2013, 10:16:05 AM4/12/13
to mongo...@googlegroups.com
Mongodb recently redid their docs and they are inferior to what they had before. I must say, I don't know what they were thinking. I was trying to find something a week or two ago and just kept going in circles on the site. I'd never had problems finding the docs for whatever it was on the old site. 

WRT MM, it's important to get familiar with Plucky, also by jnunemaker. It's responsible for generating the queries in MM.


Reply all
Reply to author
Forward
0 new messages