Class: Mongo::Collection::View::MapReduce

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Immutable, Loggable, Retryable
Defined in:
lib/mongo/collection/view/map_reduce.rb

Overview

Provides behavior around a map/reduce operation on the collection view.

Since:

  • 2.0.0

Constant Summary collapse

INLINE =

The inline option.

Since:

  • 2.1.0

'inline'.freeze
REROUTE =
Deprecated.

Reroute message.

Since:

  • 2.1.0

'Rerouting the MapReduce operation to the primary server.'.freeze

Constants included from Loggable

Loggable::PREFIX

Instance Attribute Summary collapse

Attributes included from Immutable

#options

Instance Method Summary collapse

Methods included from Retryable

#legacy_write_with_retry, #nro_write_with_retry, #read_with_one_retry, #read_with_retry, #read_with_retry_cursor, #write_with_retry

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initialize(view, map, reduce, options = {}) ⇒ MapReduce

Initialize the map/reduce for the provided collection view, functions and options.

Examples:

Create the new map/reduce view.


Parameters:

  • view (Collection::View)

    The collection view.

  • map (String)

    The map function.

  • reduce (String)

    The reduce function.

  • options (Hash) (defaults to: {})

    The map/reduce options.

Since:

  • 2.0.0



110
111
112
113
114
115
# 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

Instance Attribute Details

#map_functionString (readonly)

Returns map The map function.

Returns:

  • (String)

    map The map function.

Since:

  • 2.0.0



45
46
47
# File 'lib/mongo/collection/view/map_reduce.rb', line 45

def map_function
  @map_function
end

#reduce_functionString (readonly)

Returns reduce The reduce function.

Returns:

  • (String)

    reduce The reduce function.

Since:

  • 2.0.0



48
49
50
# File 'lib/mongo/collection/view/map_reduce.rb', line 48

def reduce_function
  @reduce_function
end

#viewView (readonly)

Returns view The collection view.

Returns:

  • (View)

    view The collection view.

Since:

  • 2.0.0



42
43
44
# File 'lib/mongo/collection/view/map_reduce.rb', line 42

def view
  @view
end

Instance Method Details

#each {|Each| ... } ⇒ Enumerator

Iterate through documents returned by the map/reduce.

Examples:

Iterate through the result of the map/reduce.

map_reduce.each do |document|
  p document
end

Yield Parameters:

  • Each (Hash)

    matching document.

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 2.0.0



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

#executeMongo::Operation::Result

Execute the map reduce, without doing a fetch query to retrieve the results

if outputted to a collection.

Examples:

Execute the map reduce and get the raw result.

map_reduce.execute

Returns:

Since:

  • 2.5.0



196
197
198
199
200
201
202
203
# 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

#finalize(function = nil) ⇒ MapReduce, String

Set or get the finalize function for the operation.

Examples:

Set the finalize function.

map_reduce.finalize(function)

Parameters:

  • function (String) (defaults to: nil)

    The finalize js function.

Returns:

  • (MapReduce, String)

    The new MapReduce operation or the value of the function.

Since:

  • 2.0.0



95
96
97
# File 'lib/mongo/collection/view/map_reduce.rb', line 95

def finalize(function = nil)
  configure(:finalize, function)
end

#js_mode(value = nil) ⇒ MapReduce, ...

Set or get the jsMode flag for the operation.

Examples:

Set js mode for the operation.

map_reduce.js_mode(true)

Parameters:

  • value (true, false) (defaults to: nil)

    The jsMode value.

Returns:

  • (MapReduce, true, false)

    The new MapReduce operation or the value of the jsMode flag.

Since:

  • 2.0.0



128
129
130
# File 'lib/mongo/collection/view/map_reduce.rb', line 128

def js_mode(value = nil)
  configure(:js_mode, value)
end

#out(location = nil) ⇒ MapReduce, Hash

Set or get the output location for the operation.

Examples:

Set the output to inline.

map_reduce.out(inline: 1)

Set the output collection to merge.

map_reduce.out(merge: 'users')

Set the output collection to replace.

map_reduce.out(replace: 'users')

Set the output collection to reduce.

map_reduce.out(reduce: 'users')

Parameters:

  • location (Hash) (defaults to: nil)

    The output location details.

Returns:

  • (MapReduce, Hash)

    The new MapReduce operation or the value of the output location.

Since:

  • 2.0.0



152
153
154
# File 'lib/mongo/collection/view/map_reduce.rb', line 152

def out(location = nil)
  configure(:out, location)
end

#scope(object = nil) ⇒ MapReduce, Hash

Set or get a scope on the operation.

Examples:

Set the scope value.

map_reduce.scope(value: 'test')

Parameters:

  • object (Hash) (defaults to: nil)

    The scope object.

Returns:

  • (MapReduce, Hash)

    The new MapReduce operation or the value of the scope.

Since:

  • 2.0.0



167
168
169
# File 'lib/mongo/collection/view/map_reduce.rb', line 167

def scope(object = nil)
  configure(:scope, object)
end

#verbose(value = nil) ⇒ MapReduce, Hash

Whether to include the timing information in the result.

Examples:

Set the verbose value.

map_reduce.verbose(false)

Parameters:

  • value (true, false) (defaults to: nil)

    Whether to include timing information in the result.

Returns:

  • (MapReduce, Hash)

    The new MapReduce operation or the value of the verbose option.

Since:

  • 2.0.5



183
184
185
# File 'lib/mongo/collection/view/map_reduce.rb', line 183

def verbose(value = nil)
  configure(:verbose, value)
end