2

I am trying to use map_reduce on a collection, via the ruby console , but I am getting "NoMethodError: undefined method `map_reduce' for #

results = Thing.collection.map_reduce(map, reduce, out: "vr")
jdog
  • 10,351
  • 29
  • 90
  • 165

1 Answers1

7

Map Reduce in Mongoid 3 works slightly differently. The syntax you have would work for the mongo ruby driver. In Mongoid 3, you call this off the class or criteria, like the following:

From a criteria:

Model.where(field: value).map_reduce(map, reduce).out(inline: true)

From a class:

SomeClass.map_reduce(map, reduce).out(replace: "mr-results").each do |document|
  #do something
end

You can find more information on this in the Mongoid docs

Andre de Frere
  • 2,703
  • 16
  • 13
  • Not sure if your answer applies. Here is what I am following http://www.packtpub.com/article/ruby-mongodb-web-developement – jdog Nov 30 '12 at 16:35
  • 1
    The example you have given is using Mongoid 2.2.1, whereas the error you are receiving refers to `Moped::Collection`. Moped is the MongoDB driver for Mongoid 3. In Mongoid 2, you would pass the map reduce to the Ruby Driver in the format you have given. In Mongoid 3, map/reduce is called from a class or criteria - not from a collection. – Andre de Frere Dec 02 '12 at 22:41