Permalink
Browse files

Support read preferences for Collection#map_reduce.

  • Loading branch information...
1 parent 090cd7e commit 735a0a0b344489146d8f4c96a7d97258edb78c72 @nelhage committed Dec 5, 2012
Showing with 11 additions and 2 deletions.
  1. +9 −1 lib/mongo/collection.rb
  2. +2 −1 lib/mongo/util/support.rb
View
@@ -692,6 +692,8 @@ def aggregate(pipeline=nil)
# the instantiated collection that's returned by default. Note if a collection name isn't returned in the
# map-reduce output (as, for example, when using :out => { :inline => 1 }), then you must specify this option
# or an ArgumentError will be raised.
+ # @option opts [:primary, :secondary] :read Read preference indicating which server to run this map-reduce
+ # on. See Collection#find for more details.
#
# @return [Collection, Hash] a Mongo::Collection object or a Hash with the map-reduce command's results.
#
@@ -705,6 +707,12 @@ def map_reduce(map, reduce, opts={})
reduce = BSON::Code.new(reduce) unless reduce.is_a?(BSON::Code)
raw = opts.delete(:raw)
+ if read_pref = opts[:read]
+ Mongo::Support.validate_read_preference(read_pref)
+ else
+ read_pref = read_preference
+ end
+
hash = BSON::OrderedHash.new
hash['mapreduce'] = self.name
hash['map'] = map
@@ -714,7 +722,7 @@ def map_reduce(map, reduce, opts={})
hash[:sort] = Mongo::Support.format_order_clause(hash[:sort])
end
- result = @db.command(hash)
+ result = @db.command(hash, :read => read_pref)
unless Mongo::Support.ok?(result)
raise Mongo::OperationFailure, "map-reduce failed: #{result['errmsg']}"
end
@@ -43,7 +43,8 @@ module Support
'distinct',
'geonear',
'geosearch',
- 'geowalk'
+ 'geowalk',
+ 'mapreduce'
]
# Generate an MD5 for authentication.

0 comments on commit 735a0a0

Please sign in to comment.