17

I am using Ruby code to calculate sum from the array returned by Mongoid.

But maybe using Map/Reduce can be faster, except I don't see any docs for Map Reduce on mongoid.org and Google for

map reduce site:mongoid.org

doesn't give any result either. (or using MapReduce or Map/Reduce)

There are docs on MongoDB's site

map reduce site:mongodb.org

but need to use Map Reduce with Mongoid as well.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740

1 Answers1

21

You can use map reduce with Mongoid just as you could through the Ruby driver directly:

# Post is a Mongoid model...
Post.collection.map_reduce(map_function, reduce_function, options)

For some examples of doing map reduce in the Ruby driver, see this blog post by Kyle Banker (maintainer of the Ruby MongoDB driver).

PreciousBodilyFluids
  • 11,881
  • 3
  • 37
  • 44
  • hm, so this is to go a lower level route by using the Ruby driver for MongoDB... not by using Mongoid per se? Maybe if Mongoid has it directly it can be better... (does having Mongoid imply you also have the Ruby driver or it has to be separate?) As of right now, if I use `Analytics.collection` it returns a `Mongoid::Collection` object. `Analytic.collection.methods.grep /map/` => `["mapreduce", "map_reduce"]`, so map_reduce is defined. – nonopolarity Sep 16 '10 at 00:53
  • 2
    Mongoid runs on top of the Ruby driver, and using the method above just passes the functions through to the Ruby driver. – PreciousBodilyFluids Sep 16 '10 at 00:54
  • so maybe Mongoid can put that into their formal docs huh? that Mongoid supports Map/Reduce by using the Ruby driver. – nonopolarity Sep 16 '10 at 01:00