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 behaviour around a map/reduce operation on the collection view.

Since:

  • 2.0.0

Constant Summary

INLINE =

The inline option.

Since:

  • 2.1.0

'inline'.freeze
REROUTE =

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

#read_with_one_retry, #read_with_retry, #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



106
107
108
109
110
111
# 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

Instance Attribute Details

#mapString (readonly)

Returns map The map function.

Returns:

  • (String)

    map The map function.

Since:

  • 2.0.0



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

def map
  @map
end

#reduceString (readonly)

Returns reduce The reduce function.

Returns:

  • (String)

    reduce The reduce function.

Since:

  • 2.0.0



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

def reduce
  @reduce
end

#viewView (readonly)

Returns view The collection view.

Returns:

  • (View)

    view The collection view.

Since:

  • 2.0.0



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

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



67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ 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



91
92
93
# File 'lib/mongo/collection/view/map_reduce.rb', line 91

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



124
125
126
# File 'lib/mongo/collection/view/map_reduce.rb', line 124

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



148
149
150
# File 'lib/mongo/collection/view/map_reduce.rb', line 148

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



163
164
165
# File 'lib/mongo/collection/view/map_reduce.rb', line 163

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



179
180
181
# File 'lib/mongo/collection/view/map_reduce.rb', line 179

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