Skip to content

Instantly share code, notes, and snippets.

@banker
Created September 1, 2011 19:22
Show Gist options
  • Save banker/1187018 to your computer and use it in GitHub Desktop.
Save banker/1187018 to your computer and use it in GitHub Desktop.
map_reduce_fix.rb
module Mongo
class Collection
def map_reduce(map, reduce, opts={})
map = BSON::Code.new(map) unless map.is_a?(BSON::Code)
reduce = BSON::Code.new(reduce) unless reduce.is_a?(BSON::Code)
raw = opts.delete(:raw)
hash = BSON::OrderedHash.new
hash['mapreduce'] = self.name
hash['map'] = map
hash['reduce'] = reduce
hash.merge! opts
result = @db.command(hash)
unless Mongo::Support.ok?(result)
raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
end
if raw
result
elsif result["result"]
@db[result["result"]]
else
raise ArgumentError, "Could not instantiate collection from result. If you specified " +
"{:out => {:inline => true}}, then you must also specify :raw => true to get the results."
end
end
alias :mapreduce :map_reduce
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment