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:
I can roll this into my VisitorSession model:
This obviously makes it easier to call:
>> VisitorSession.first(:conditions => {:project_id => '2f5178'}).visits_for_project
=> 1
Sorry to be a smartass, but EOF is end-of-file while you probably mean EOS end-of-string.
The heredoc delimiter is an arbitrary string so I could equally have used:
Of course It would make sense to use something descriptive, so End Of Function will have to do :)
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…
Also, I approve of your use of EOF. It’s a convention.
EOF
for the sake of readability I use this style of code:
map = <<MAP
function() {
……
}
MAP
reduce = <<REDUCE
function(key, values) {
…….
};
REDUCE
great example, but it doesn’t seem to be the correct way to pre-filter your collection:
if (this.project_id == ‘#{project_id}’) {
and the correct way is to use a query option when calling mapreduce. Mongoid supports options as 3rd parameter in this function (as in js documentation).
It won’t use index in your example, but with this correction – it will.