Skip to content

@fredrikhenne /group_sum.rb forked from jnunemaker/group_sum.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
require 'rubygems'
require 'mongo'
require 'pp'
db = Mongo::Connection.new.db('testing')
db.collections.each { |c| c.remove }
articles = db['articles']
ratings = db['ratings']
article_id = articles.save(:title => 'Foo')
ratings.save(:article_id => article_id, :rating => 2)
ratings.save(:article_id => article_id, :rating => 3)
ratings.save(:article_id => article_id, :rating => 5)
keys = [:article_id]
query = {:article_id => article_id}
initial = {:sum => 0}
function = "function(doc, prev) { prev.sum += doc.rating; }"
pp ratings.group(keys, query, initial, function)
__END__
[{"article_id"=>$oid4be315c9a8de309439000001, "sum"=>10.0}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.