class Mongo::Collection::View::MapReduce
Provides behavior around a map/reduce operation on the collection view.
@since 2.0.0
Constants
- INLINE
The inline option.
@since 2.1.0
- REROUTE
Reroute message.
@since 2.1.0 @deprecated
Attributes
@return [ String ] map The map function.
@return [ String ] reduce The reduce function.
@return [ View
] view The collection view.
Public Class Methods
Initialize the map/reduce for the provided collection view, functions and options.
@example Create the new map/reduce view.
@param [ Collection::View
] view The collection view. @param [ String ] map The map function. @param [ String ] reduce The reduce function. @param [ Hash ] options The map/reduce options.
@since 2.0.0
# File lib/mongo/collection/view/map_reduce.rb, line 110 def initialize(view, map, reduce, options = {}) @view = view @map_function = map.dup.freeze @reduce_function = reduce.dup.freeze @options = BSON::Document.new(options).freeze end
Public Instance Methods
Iterate through documents returned by the map/reduce.
@example Iterate through the result of the map/reduce.
map_reduce.each do |document| p document end
@return [ Enumerator ] The enumerator.
@since 2.0.0
@yieldparam [ Hash ] Each matching document.
# File lib/mongo/collection/view/map_reduce.rb, line 68 def each @cursor = nil session = client.send(:get_session, @options) server = cluster.next_primary(nil, session) result = send_initial_query(server, session) result = send_fetch_query(server, session) unless inline? @cursor = Cursor.new(view, result, server, session: session) if block_given? @cursor.each do |doc| yield doc end else @cursor.to_enum end end
Execute the map reduce, without doing a fetch query to retrieve the results
if outputted to a collection.
@example Execute the map reduce and get the raw result.
map_reduce.execute
@return [ Mongo::Operation::Result
] The raw map reduce result
@since 2.5.0
# File lib/mongo/collection/view/map_reduce.rb, line 196 def execute view.send(:with_session, @options) do |session| write_concern = view.write_concern_with_session(session) nro_write_with_retry(session, write_concern) do |server| send_initial_query(server, session) end end end
Set or get the finalize function for the operation.
@example Set the finalize function.
map_reduce.finalize(function)
@param [ String ] function The finalize js function.
@return [ MapReduce
, String ] The new MapReduce
operation or the
value of the function.
@since 2.0.0
# File lib/mongo/collection/view/map_reduce.rb, line 95 def finalize(function = nil) configure(:finalize, function) end
Set or get the jsMode flag for the operation.
@example Set js mode for the operation.
map_reduce.js_mode(true)
@param [ true, false ] value The jsMode value.
@return [ MapReduce
, true, false ] The new MapReduce
operation or the
value of the jsMode flag.
@since 2.0.0
# File lib/mongo/collection/view/map_reduce.rb, line 128 def js_mode(value = nil) configure(:js_mode, value) end
Set or get the output location for the operation.
@example Set the output to inline.
map_reduce.out(inline: 1)
@example Set the output collection to merge.
map_reduce.out(merge: 'users')
@example Set the output collection to replace.
map_reduce.out(replace: 'users')
@example Set the output collection to reduce.
map_reduce.out(reduce: 'users')
@param [ Hash ] location The output location details.
@return [ MapReduce
, Hash ] The new MapReduce
operation or the value
of the output location.
@since 2.0.0
# File lib/mongo/collection/view/map_reduce.rb, line 152 def out(location = nil) configure(:out, location) end
Set or get a scope on the operation.
@example Set the scope value.
map_reduce.scope(value: 'test')
@param [ Hash ] object The scope object.
@return [ MapReduce
, Hash ] The new MapReduce
operation or the value
of the scope.
@since 2.0.0
# File lib/mongo/collection/view/map_reduce.rb, line 167 def scope(object = nil) configure(:scope, object) end
Whether to include the timing information in the result.
@example Set the verbose value.
map_reduce.verbose(false)
@param [ true, false ] value Whether to include timing information
in the result.
@return [ MapReduce
, Hash ] The new MapReduce
operation or the value
of the verbose option.
@since 2.0.5
# File lib/mongo/collection/view/map_reduce.rb, line 183 def verbose(value = nil) configure(:verbose, value) end
Private Instance Methods
# File lib/mongo/collection/view/map_reduce.rb, line 253 def fetch_query_op(server, session) if server.features.find_command_enabled? Operation::Find.new(find_command_spec(session)) else Operation::Find.new(fetch_query_spec) end end
# File lib/mongo/collection/view/map_reduce.rb, line 245 def fetch_query_spec Builder::MapReduce.new(map_function, reduce_function, view, options).query_specification end
# File lib/mongo/collection/view/map_reduce.rb, line 249 def find_command_spec(session) Builder::MapReduce.new(map_function, reduce_function, view, options.merge(session: session)).command_specification end
# File lib/mongo/collection/view/map_reduce.rb, line 223 def initial_query_op(session) Operation::MapReduce.new(map_reduce_spec(session)) end
# File lib/mongo/collection/view/map_reduce.rb, line 211 def inline? out.nil? || out == { inline: 1 } || out == { INLINE => 1 } end
# File lib/mongo/collection/view/map_reduce.rb, line 215 def map_reduce_spec(session = nil) Builder::MapReduce.new(map_function, reduce_function, view, options.merge(session: session)).specification end
# File lib/mongo/collection/view/map_reduce.rb, line 219 def new(options) MapReduce.new(view, map_function, reduce_function, options) end
# File lib/mongo/collection/view/map_reduce.rb, line 231 def secondary_ok? out.respond_to?(:keys) && out.keys.first.to_s.downcase == INLINE end
# File lib/mongo/collection/view/map_reduce.rb, line 261 def send_fetch_query(server, session) fetch_query_op(server, session).execute(server) end
# File lib/mongo/collection/view/map_reduce.rb, line 235 def send_initial_query(server, session) unless valid_server?(server) msg = "Rerouting the MapReduce operation to the primary server - #{server.summary} is not suitable" log_warn(msg) server = cluster.next_primary(nil, session) end validate_collation!(server) initial_query_op(session).execute(server) end
# File lib/mongo/collection/view/map_reduce.rb, line 207 def server_selector @view.send(:server_selector) end
# File lib/mongo/collection/view/map_reduce.rb, line 227 def valid_server?(server) server.standalone? || server.mongos? || server.primary? || secondary_ok? end
# File lib/mongo/collection/view/map_reduce.rb, line 265 def validate_collation!(server) if (view.options[:collation] || options[:collation]) && !server.features.collation_enabled? raise Error::UnsupportedCollation.new end end