Skip to content

Commit 4f3937d

Browse files
committed
Map-reduce doc update for v1.8
1 parent fa0a933 commit 4f3937d

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/mongo/collection.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def find_and_modify(opts={})
509509
@db.command(cmd)['value']
510510
end
511511

512-
# Perform a map/reduce operation on the current collection.
512+
# Perform a map-reduce operation on the current collection.
513513
#
514514
# @param [String, BSON::Code] map a map function, written in JavaScript.
515515
# @param [String, BSON::Code] reduce a reduce function, written in JavaScript.
@@ -529,11 +529,13 @@ def find_and_modify(opts={})
529529
# @option opts [Boolean ] :verbose (false) if true, provides statistics on job execution time.
530530
# @option opts [Boolean] :raw (false) if true, return the raw result object from the map_reduce command, and not
531531
# the instantiated collection that's returned by default. Note if a collection name isn't returned in the
532-
# map-reduce output (as, for example, when using :out => {:inline => 1}), then this option will be
533-
# forced to true.
532+
# map-reduce output (as, for example, when using :out => {:inline => 1}), then you must specify this option
533+
# or an ArgumentError will be raised.
534534
#
535535
# @return [Collection, Hash] a Mongo::Collection object or a Hash with the map-reduce command's results.
536536
#
537+
# @raise ArgumentError if you specify {:out => {:inline => true}} but don't specify :raw => true.
538+
#
537539
# @see http://www.mongodb.org/display/DOCS/MapReduce Offical MongoDB map/reduce documentation.
538540
#
539541
# @core mapreduce map_reduce-instance_method
@@ -553,10 +555,13 @@ def map_reduce(map, reduce, opts={})
553555
raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
554556
end
555557

556-
if raw || !result["result"]
558+
if raw
557559
result
558-
else
560+
elsif result["result"]
559561
@db[result["result"]]
562+
else
563+
raise ArgumentError, "Could not instantiate collection from result. If you specified " +
564+
"{:out => {:inline => true}}, then you must also specify :raw => true to get the results."
560565
end
561566
end
562567
alias :mapreduce :map_reduce

test/collection_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,11 @@ def test_map_reduce_with_collection_merge
485485
res = @@test.map_reduce(m, r, :out => {:reduce => output_collection})
486486
assert res.find.to_a.any? {|doc| doc["_id"] == 3 && doc["value"]["count"] == 2}
487487

488-
res = @@test.map_reduce(m, r, :out => {:inline => 1})
488+
assert_raise ArgumentError do
489+
@@test.map_reduce(m, r, :out => {:inline => 1})
490+
end
491+
492+
@@test.map_reduce(m, r, :raw => true, :out => {:inline => 1})
489493
assert res["results"]
490494
end
491495
end

0 commit comments

Comments
 (0)