RUBY-144 allow map_reduce to return raw command (and not just instantiated results collection)

This commit is contained in:
Kyle Banker 2010-07-09 15:29:11 -04:00
parent f22e81414b
commit c25f1f5997
2 changed files with 28 additions and 2 deletions

View File

@ -470,6 +470,8 @@ module Mongo
# @option opts [String] :out (nil) the name of the output collection. If specified, the collection will not be treated as temporary.
# @option opts [Boolean] :keeptemp (false) if true, the generated collection will be persisted. default is false.
# @option opts [Boolean ] :verbose (false) if true, provides statistics on job execution time.
# @options opts [Boolean] :raw (false) if true, return the raw result object from the map_reduce command, and not
# the instantiated collection that's returned by default.
#
# @return [Collection] a collection containing the results of the operation.
#
@ -477,8 +479,10 @@ module Mongo
#
# @core mapreduce map_reduce-instance_method
def map_reduce(map, reduce, opts={})
opts.assert_valid_keys(:query, :sort, :limit, :finalize, :keeptemp, :verbose, :raw)
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
@ -490,8 +494,13 @@ module Mongo
unless Mongo::Support.ok?(result)
raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
end
if raw
result
else
@db[result["result"]]
end
end
alias :mapreduce :map_reduce
# Perform a group aggregation.

View File

@ -393,7 +393,7 @@ class TestCollection < Test::Unit::TestCase
assert c.closed?
end
if @@version < "1.1.1"
if @@version > "1.1.1"
def test_map_reduce
@@test << { "user_id" => 1 }
@@test << { "user_id" => 2 }
@ -429,6 +429,23 @@ class TestCollection < Test::Unit::TestCase
assert res.find_one({"_id" => 2})
assert res.find_one({"_id" => 3})
end
def test_map_reduce_with_raw_response
m = Code.new("function() { emit(this.user_id, 1); }")
r = Code.new("function(k,vals) { return 1; }")
res = @@test.map_reduce(m, r, :raw => true)
assert res["result"]
assert res["counts"]
assert res["timeMillis"]
end
def test_allows_only_valid_keys
m = Code.new("function() { emit(this.user_id, 1); }")
r = Code.new("function(k,vals) { return 1; }")
assert_raise ArgumentError do
@@test.map_reduce(m, r, :foo => true)
end
end
end
if @@version > "1.3.0"