Skip to content

Instantly share code, notes, and snippets.

@superp
Created July 6, 2011 13:57
Show Gist options
  • Save superp/1067281 to your computer and use it in GitHub Desktop.
Save superp/1067281 to your computer and use it in GitHub Desktop.
Group by example in MongoMapper
class Tweet
include MongoMapper::Document
key :id_str, String
key :from_user_id, Integer
belongs_to :track_item
def self.grouped_by(column, options = {})
map_function = "function() { emit( this.#{column}, 1); }"
# put your logic here (not needed in my case)
reduce_function = %Q( function(key, values) {
return true;
})
collection.map_reduce(map_function, reduce_function, options)
end
end
Tweet.grouped_by(:from_user_id, :query => {:track_item_id => track_item.id}).find().count
@marcusleemitchell
Copy link

I get what you're doing there but I get:
The error occurred while evaluating nil.last
/Users/mmitchell/.rbenv/versions/ree-1.8.7-2011.03/lib/ruby/gems/1.8/gems/mongo-1.8.2/lib/mongo/util/support.rb:83:in `secondary_ok?'

Any ideas on this?
Many thanks

@bmulvihill
Copy link

undefined method `last' for nil:NilClass

I get this error when I try and run map reduce, any idea why?

@ringe
Copy link

ringe commented Mar 17, 2014

I get the same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment