It's all good

map-reduce using mongoid

It took me a while to work out how to use map-reduce in Ruby using mongoid so I thought I’d share it here in-case it helps anyone else get there quicker. I start out with a model that includes Mongoid::Document:

To map-reduce across the collection I need to define a map and reduce function in javascript then run the on the collection:

That’s a useful post, thanks. It’s made me realise that map/reduce won’t solve my problem, as I’d need my map function to iterate over all the embededded documents in a single object. Time to write an iterator, I think…