Class: Mongoid::Contextual::MapReduce

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Command
Defined in:
lib/mongoid/contextual/map_reduce.rb

Instance Attribute Summary

Attributes included from Command

#collection, #collection The collection to query against., #criteria, #criteria The criteria for the context.

Instance Method Summary collapse

Methods included from Command

#command, #session

Constructor Details

#initialize(collection, criteria, map, reduce) ⇒ MapReduce

Initialize the new map/reduce directive.

Examples:

Initialize the new map/reduce.

MapReduce.new(criteria, map, reduce)

Parameters:

  • criteria (Criteria)

    The Mongoid criteria.

  • map (String)

    The map js function.

  • reduce (String)

    The reduce js function.

Since:

  • 3.0.0



81
82
83
84
85
86
# File 'lib/mongoid/contextual/map_reduce.rb', line 81

def initialize(collection, criteria, map, reduce)
  @collection, @criteria = collection, criteria
  command[:mapreduce] = collection.name.to_s
  command[:map], command[:reduce] = map, reduce
  apply_criteria_options
end

Instance Method Details

#countsHash

Get all the counts returned by the map/reduce.

Examples:

Get the counts.

map_reduce.counts

Returns:

  • (Hash)

    The counts.

Since:

  • 3.0.0



19
20
21
# File 'lib/mongoid/contextual/map_reduce.rb', line 19

def counts
  results["counts"]
end

#eachEnumerator

Iterates over each of the documents in the map/reduce, excluding the extra information that was passed back from the database.

Examples:

Iterate over the results.

map_reduce.each do |doc|
  p doc
end

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 3.0.0



34
35
36
37
38
39
40
41
42
# File 'lib/mongoid/contextual/map_reduce.rb', line 34

def each
  if block_given?
    documents.each do |doc|
      yield doc
    end
  else
    to_enum
  end
end

#emittedInteger

Get the number of documents emitted by the map/reduce.

Examples:

Get the emitted document count.

map_reduce.emitted

Returns:

  • (Integer)

    The number of emitted documents.

Since:

  • 3.0.0



52
53
54
# File 'lib/mongoid/contextual/map_reduce.rb', line 52

def emitted
  counts["emit"]
end

#finalize(function) ⇒ MapReduce

Provide a finalize js function for the map/reduce.

Examples:

Provide a finalize function.

map_reduce.finalize(func)

Parameters:

  • function (String)

    The finalize function.

Returns:

Since:

  • 3.0.0



66
67
68
69
# File 'lib/mongoid/contextual/map_reduce.rb', line 66

def finalize(function)
  command[:finalize] = function
  self
end

#inputInteger

Get the number of documents that were input into the map/reduce.

Examples:

Get the count of input documents.

map_reduce.input

Returns:

  • (Integer)

    The number of input documents.

Since:

  • 3.0.0



96
97
98
# File 'lib/mongoid/contextual/map_reduce.rb', line 96

def input
  counts["input"]
end

#inspectString

Get a pretty string representation of the map/reduce, including the criteria, map, reduce, finalize, and out option.

Examples:

Inspect the map_reduce.

map_reduce.inspect

Returns:

  • (String)

    The inspection string.

Since:

  • 3.1.0



224
225
226
227
228
229
230
231
232
233
# File 'lib/mongoid/contextual/map_reduce.rb', line 224

def inspect
%Q{#<Mongoid::Contextual::MapReduce
  selector: #{criteria.selector.inspect}
  class:    #{criteria.klass}
  map:      #{command[:map]}
  reduce:   #{command[:reduce]}
  finalize: #{command[:finalize]}
  out:      #{command[:out].inspect}>
}
end

#js_modeMapReduce

Sets the map/reduce to use jsMode.

Examples:

Set the map/reduce to jsMode.

map_reduce.js_mode

Returns:

Since:

  • 3.0.0



108
109
110
111
# File 'lib/mongoid/contextual/map_reduce.rb', line 108

def js_mode
  command[:jsMode] = true
  self
end

#out(location) ⇒ MapReduce

Specifies where the map/reduce output is to be stored.

Examples:

Store output in memory.

map_reduce.out(inline: 1)

Store output in a collection, replacing existing documents.

map_reduce.out(replace: "collection_name")

Store output in a collection, merging existing documents.

map_reduce.out(merge: "collection_name")

Store output in a collection, reducing existing documents.

map_reduce.out(reduce: "collection_name")

Parameters:

  • location (Hash)

    The place to store the results.

Returns:

Since:

  • 3.0.0



132
133
134
135
136
137
138
139
# File 'lib/mongoid/contextual/map_reduce.rb', line 132

def out(location)
  normalized = location.dup
  normalized.update_values do |value|
    value.is_a?(::Symbol) ? value.to_s : value
  end
  command[:out] = normalized
  self
end

#outputInteger

Get the number of documents output by the map/reduce.

Examples:

Get the output document count.

map_reduce.output

Returns:

  • (Integer)

    The number of output documents.

Since:

  • 3.0.0



149
150
151
# File 'lib/mongoid/contextual/map_reduce.rb', line 149

def output
  counts["output"]
end

#rawHash Also known as: execute

Get the raw output from the map/reduce operation.

Examples:

Get the raw output.

map_reduce.raw

Returns:

  • (Hash)

    The raw output.

Since:

  • 3.0.0



161
162
163
# File 'lib/mongoid/contextual/map_reduce.rb', line 161

def raw
  results
end

#reducedInteger

Get the number of documents reduced by the map/reduce.

Examples:

Get the reduced document count.

map_reduce.reduced

Returns:

  • (Integer)

    The number of reduced documents.

Since:

  • 3.0.0



184
185
186
# File 'lib/mongoid/contextual/map_reduce.rb', line 184

def reduced
  counts["reduce"]
end

#scope(object) ⇒ MapReduce

Adds a javascript object to the global scope of the map/reduce.

Examples:

Add an object to the global scope.

map_reduce.scope(name: value)

Parameters:

  • object (Hash)

    A hash of key/values for the global scope.

Returns:

Since:

  • 3.0.0



198
199
200
201
# File 'lib/mongoid/contextual/map_reduce.rb', line 198

def scope(object)
  command[:scope] = object
  self
end

#timeFloat

Get the execution time of the map/reduce.

Examples:

Get the execution time.

map_reduce.time

Returns:

  • (Float)

    The time in milliseconds.

Since:

  • 3.0.0



211
212
213
# File 'lib/mongoid/contextual/map_reduce.rb', line 211

def time
  results["timeMillis"]
end