123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Contextual::MapReduce

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Instance Chain:
self, Command, Enumerable
Inherits: Object
Defined in: lib/mongoid/contextual/map_reduce.rb

Overview

Represents a mapReduce database command instruction.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Command - Included

#client

Get the database client.

#command

The database command that is being built to send to the db.

Constructor Details

.new(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.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 77

def initialize(collection, criteria, map, reduce)
  @collection = collection
  @criteria = criteria
  @map_reduce = @criteria.view.map_reduce(map, reduce)
end

Instance Method Details

#_session (private)

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 231

def _session
  criteria.send(:_session)
end

#commandHash

Returns the selector of the command spec.

Returns:

  • (Hash)

    The selector.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 221

def command
  @map_reduce.send(:map_reduce_spec)[:selector]
end

#countsHash

Get all the counts returned by the map/reduce.

Examples:

Get the counts.

map_reduce.counts

Returns:

  • (Hash)

    The counts.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 22

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.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 35

def each
  validate_out!
  if block_given?
    @map_reduce.each do |doc|
      yield doc
    end
  else
    @map_reduce.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.

[ GitHub ]

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

def emitted
  counts["emit"]
end

#execute

Alias for #raw.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 165

alias :execute :raw

#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:

  • (MapReduce)

    The map reduce.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 64

def finalize(function)
  @map_reduce = @map_reduce.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.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 89

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.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 207

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:

  • (MapReduce)

    The map/reduce.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 99

def js_mode
  @map_reduce = @map_reduce.js_mode(true)
  self
end

#out(location) ⇒ MapReduce

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

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")

Return results from map reduce.

map_reduce.out(inline: 1)

Parameters:

  • location (Hash)

    The place to store the results.

Returns:

  • (MapReduce)

    The map/reduce object.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 125

def out(location)
  normalized = location.dup
  normalized.transform_values! do |value|
    value.is_a?(::Symbol) ? value.to_s : value
  end
  @map_reduce = @map_reduce.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.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 140

def output
  counts["output"]
end

#rawHash Also known as: #results, #execute

Get the raw output from the map/reduce operation.

Examples:

Get the raw output.

map_reduce.raw

Returns:

  • (Hash)

    The raw output.

[ GitHub ]

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

def raw
  validate_out!
  cmd = command
  opts = { read: criteria.options.fetch(:read) } if criteria.options[:read]
  @map_reduce.database.command(cmd, (opts || {}).merge(session: _session)).first
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.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 173

def reduced
  counts["reduce"]
end

#results

Alias for #raw.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 156

alias :results :raw

#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.

[ GitHub ]

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

def scope(object)
  @map_reduce = @map_reduce.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.

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 196

def time
  results["timeMillis"]
end

#validate_out! (private)

[ GitHub ]

  
# File 'lib/mongoid/contextual/map_reduce.rb', line 227

def validate_out!
  raise Errors::NoMapReduceOutput.new({}) unless @map_reduce.out
end