class Mongo::Collection::View::MapReduce

Provides behaviour 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

Attributes

map[R]

@return [ String ] map The map function.

reduce[R]

@return [ String ] reduce The reduce function.

view[R]

@return [ View ] view The collection view.

Public Class Methods

new(view, map, reduce, options = {}) click to toggle source

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 106
def initialize(view, map, reduce, options = {})
  @view = view
  @map = map.freeze
  @reduce = reduce.freeze
  @options = BSON::Document.new(options).freeze
end

Public Instance Methods

each() { |doc| ... } click to toggle source

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 67
def each
  @cursor = nil
  write_with_retry do
    server = read.select_server(cluster, false)
    result = send_initial_query(server)
    @cursor = Cursor.new(view, result, server)
  end
  @cursor.each do |doc|
    yield doc
  end if block_given?
  @cursor.to_enum
end
finalize(function = nil) click to toggle source

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 91
def finalize(function = nil)
  configure(:finalize, function)
end
js_mode(value = nil) click to toggle source

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 124
def js_mode(value = nil)
  configure(:js_mode, value)
end
out(location = nil) click to toggle source

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 148
def out(location = nil)
  configure(:out, location)
end
scope(object = nil) click to toggle source

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 163
def scope(object = nil)
  configure(:scope, object)
end
verbose(value = nil) click to toggle source

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 179
def verbose(value = nil)
  configure(:verbose, value)
end

Private Instance Methods

fetch_query_op(server) click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 227
def fetch_query_op(server)
  if server.features.find_command_enabled?
    Operation::Commands::Find.new(find_command_spec)
  else
    Operation::Read::Query.new(fetch_query_spec)
  end
end
fetch_query_spec() click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 219
def fetch_query_spec
  Builder::MapReduce.new(map, reduce, view, options).query_specification
end
find_command_spec() click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 223
def find_command_spec
  Builder::MapReduce.new(map, reduce, view, options).command_specification
end
initial_query_op() click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 197
def initial_query_op
  Operation::Commands::MapReduce.new(map_reduce_spec)
end
inline?() click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 185
def inline?
  out.nil? || out == { inline: 1 } || out == { INLINE => 1 }
end
map_reduce_spec() click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 189
def map_reduce_spec
  Builder::MapReduce.new(map, reduce, view, options).specification
end
new(options) click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 193
def new(options)
  MapReduce.new(view, map, reduce, options)
end
secondary_ok?() click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 205
def secondary_ok?
  out.respond_to?(:keys) && out.keys.first.to_s.downcase == INLINE
end
send_fetch_query(server) click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 235
def send_fetch_query(server)
  fetch_query_op(server).execute(server)
end
send_initial_query(server) click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 209
def send_initial_query(server)
  unless valid_server?(server)
    log_warn(REROUTE)
    server = cluster.next_primary(false)
  end
  validate_collation!(server)
  result = initial_query_op.execute(server)
  inline? ? result : send_fetch_query(server)
end
valid_server?(server) click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 201
def valid_server?(server)
  server.standalone? || server.mongos? || server.primary? || secondary_ok?
end
validate_collation!(server) click to toggle source
# File lib/mongo/collection/view/map_reduce.rb, line 239
def validate_collation!(server)
  if (view.options[:collation] || options[:collation]) && !server.features.collation_enabled?
    raise Error::UnsupportedCollation.new
  end
end