Skip to content

Instantly share code, notes, and snippets.

@sunny0425
Created December 3, 2012 08:40
Show Gist options
  • Save sunny0425/4193689 to your computer and use it in GitHub Desktop.
Save sunny0425/4193689 to your computer and use it in GitHub Desktop.
user group in mongoid
results = Letter.collection.group({
:key => 'author_id',
:initial => {:count => 0},
:reduce => "function(x, y) { y.count++;}"
})
ViewLog.only(:loggable_id).aggregate
=> [{"loggable_id"=>BSON::ObjectId('5062678e0a217501dd000015'), "count"=>2.0},
{"loggable_id"=>BSON::ObjectId('506267e00a217501dd000084'), "count"=>2.0},
{"loggable_id"=>BSON::ObjectId('508fa73c0a21750e74000051'), "count"=>25.0},
{"loggable_id"=>BSON::ObjectId('505bec4e0a21750383000003'), "count"=>1.0},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment