class Mongoid::Contextual::MapReduce

Public Class Methods

new(collection, criteria, map, reduce) click to toggle source

Initialize the new map/reduce directive.

@example Initialize the new map/reduce.

MapReduce.new(criteria, map, reduce)

@param [ Criteria ] criteria The Mongoid criteria. @param [ String ] map The map js function. @param [ String ] reduce The reduce js function.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 85
def initialize(collection, criteria, map, reduce)
  @collection = collection
  @criteria = criteria
  @map_reduce = @criteria.view.map_reduce(map, reduce)
end

Public Instance Methods

command() click to toggle source
# File lib/mongoid/contextual/map_reduce.rb, line 246
def command
  @map_reduce.send(:map_reduce_spec)[:selector]
end
counts() click to toggle source

Get all the counts returned by the map/reduce.

@example Get the counts.

map_reduce.counts

@return [ Hash ] The counts.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 22
def counts
  results["counts"]
end
each() { |doc| ... } click to toggle source

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

@example Iterate over the results.

map_reduce.each do |doc|
  p doc
end

@return [ Enumerator ] The enumerator.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 37
def each
  validate_out!
  if block_given?
    @map_reduce.each do |doc|
      yield doc
    end
  else
    @map_reduce.to_enum
  end
end
emitted() click to toggle source

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

@example Get the emitted document count.

map_reduce.emitted

@return [ Integer ] The number of emitted documents.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 56
def emitted
  counts["emit"]
end
execute()

Execute the map/reduce, returning the raw output. Useful when you don’t care about map/reduce’s output.

@example Run the map reduce

map_reduce.execute

@return [ Hash ] The raw output

@since 3.1.0

Alias for: raw
finalize(function) click to toggle source

Provide a finalize js function for the map/reduce.

@example Provide a finalize function.

map_reduce.finalize(func)

@param [ String ] function The finalize function.

@return [ MapReduce ] The map reduce.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 70
def finalize(function)
  @map_reduce = @map_reduce.finalize(function)
  self
end
input() click to toggle source

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

@example Get the count of input documents.

map_reduce.input

@return [ Integer ] The number of input documents.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 99
def input
  counts["input"]
end
inspect() click to toggle source

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

@example Inspect the map_reduce.

map_reduce.inspect

@return [ String ] The inspection string.

@since 3.1.0

# File lib/mongoid/contextual/map_reduce.rb, line 235
      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_mode() click to toggle source

Sets the map/reduce to use jsMode.

@example Set the map/reduce to jsMode.

map_reduce.js_mode

@return [ MapReduce ] The map/reduce.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 111
def js_mode
  @map_reduce = @map_reduce.js_mode(true)
  self
end
out(location) click to toggle source

Specifies where the map/reduce output is to be stored. Please see MongoDB documentation for supported map reduce options.

@example Store output in memory.

map_reduce.out(inline: 1)

@example Store output in a collection, replacing existing documents.

map_reduce.out(replace: "collection_name")

@example Store output in a collection, merging existing documents.

map_reduce.out(merge: "collection_name")

@example Store output in a collection, reducing existing documents.

map_reduce.out(reduce: "collection_name")

@example Return results from map reduce.

map_reduce.out(inline: 1)

@param [ Hash ] location The place to store the results.

@return [ MapReduce ] The map/reduce object.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 139
def out(location)
  normalized = location.dup
  normalized.update_values do |value|
    value.is_a?(::Symbol) ? value.to_s : value
  end
  @map_reduce = @map_reduce.out(normalized)
  self
end
output() click to toggle source

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

@example Get the output document count.

map_reduce.output

@return [ Integer ] The number of output documents.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 156
def output
  counts["output"]
end
raw() click to toggle source

Get the raw output from the map/reduce operation.

@example Get the raw output.

map_reduce.raw

@return [ Hash ] The raw output.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 168
def raw
  validate_out!
  cmd = command
  opts = { read: cmd.delete(:read) } if cmd[:read]
  @map_reduce.database.command(cmd, (opts || {}).merge(session: _session)).first
end
Also aliased as: results, execute
reduced() click to toggle source

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

@example Get the reduced document count.

map_reduce.reduced

@return [ Integer ] The number of reduced documents.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 195
def reduced
  counts["reduce"]
end
results()
Alias for: raw
scope(object) click to toggle source

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

@example Add an object to the global scope.

map_reduce.scope(name: value)

@param [ Hash ] object A hash of key/values for the global scope.

@return [ MapReduce ]

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 209
def scope(object)
  @map_reduce = @map_reduce.scope(object)
  self
end
time() click to toggle source

Get the execution time of the map/reduce.

@example Get the execution time.

map_reduce.time

@return [ Float ] The time in milliseconds.

@since 3.0.0

# File lib/mongoid/contextual/map_reduce.rb, line 222
def time
  results["timeMillis"]
end

Private Instance Methods

_session() click to toggle source
# File lib/mongoid/contextual/map_reduce.rb, line 256
def _session
  criteria.send(:_session)
end
validate_out!() click to toggle source
# File lib/mongoid/contextual/map_reduce.rb, line 252
def validate_out!
  raise Errors::NoMapReduceOutput.new({}) unless @map_reduce.out
end