Created
September 1, 2011 19:22
-
-
Save banker/1187018 to your computer and use it in GitHub Desktop.
map_reduce_fix.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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