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
-
.new(collection, criteria, map, reduce) ⇒ MapReduce
constructor
Initialize the new map/reduce directive.
Instance Attribute Summary
Command
- Included
Instance Method Summary
-
#command ⇒ Hash
Returns the selector of the command spec.
-
#counts ⇒ Hash
Get all the counts returned by the map/reduce.
-
#each ⇒ Enumerator
Iterates over each of the documents in the map/reduce, excluding the extra information that was passed back from the database.
-
#emitted ⇒ Integer
Get the number of documents emitted by the map/reduce.
-
#execute
Alias for #raw.
-
#finalize(function) ⇒ MapReduce
Provide a finalize js function for the map/reduce.
-
#input ⇒ Integer
Get the number of documents that were input into the map/reduce.
-
#inspect ⇒ String
Get a pretty string representation of the map/reduce, including the criteria, map, reduce, finalize, and out option.
-
#js_mode ⇒ MapReduce
Sets the map/reduce to use jsMode.
-
#out(location) ⇒ MapReduce
Specifies where the map/reduce output is to be stored.
-
#output ⇒ Integer
Get the number of documents output by the map/reduce.
-
#raw ⇒ Hash
(also: #results, #execute)
Get the raw output from the map/reduce operation.
-
#reduced ⇒ Integer
Get the number of documents reduced by the map/reduce.
-
#results
Alias for #raw.
-
#scope(object) ⇒ MapReduce
Adds a javascript object to the global scope of the map/reduce.
-
#time ⇒ Float
Get the execution time of the map/reduce.
- #_session private
- #validate_out! private
Command
- Included
Constructor Details
.new(collection, criteria, map, reduce) ⇒ MapReduce
Initialize the new map/reduce directive.
# 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
#command ⇒ Hash
Returns the selector of the command spec.
# File 'lib/mongoid/contextual/map_reduce.rb', line 221
def command @map_reduce.send(:map_reduce_spec)[:selector] end
#counts ⇒ Hash
Get all the counts returned by the map/reduce.
# File 'lib/mongoid/contextual/map_reduce.rb', line 22
def counts results["counts"] end
#each ⇒ Enumerator
Iterates over each of the documents in the map/reduce, excluding the extra information that was passed back from the database.
# 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
#emitted ⇒ Integer
Get the number of documents emitted by the map/reduce.
# File 'lib/mongoid/contextual/map_reduce.rb', line 52
def emitted counts["emit"] end
#execute
Alias for #raw.
# File 'lib/mongoid/contextual/map_reduce.rb', line 165
alias :execute :raw
#finalize(function) ⇒ MapReduce
Provide a finalize js function for the map/reduce.
# File 'lib/mongoid/contextual/map_reduce.rb', line 64
def finalize(function) @map_reduce = @map_reduce.finalize(function) self end
#input ⇒ Integer
Get the number of documents that were input into the map/reduce.
# File 'lib/mongoid/contextual/map_reduce.rb', line 89
def input counts["input"] end
#inspect ⇒ String
Get a pretty string representation of the map/reduce, including the criteria, map, reduce, finalize, and out option.
#js_mode ⇒ MapReduce
Sets the map/reduce to use jsMode.
# 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.
# 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
#output ⇒ Integer
Get the number of documents output by the map/reduce.
# File 'lib/mongoid/contextual/map_reduce.rb', line 140
def output counts["output"] end
#raw ⇒ Hash Also known as: #results, #execute
Get the raw output from the map/reduce operation.
# File 'lib/mongoid/contextual/map_reduce.rb', line 150
def raw validate_out! cmd = command opts = { read: criteria. .fetch(:read) } if criteria. [:read] @map_reduce.database.command(cmd, (opts || {}).merge(session: _session)).first end
#reduced ⇒ Integer
Get the number of documents reduced by the map/reduce.
# File 'lib/mongoid/contextual/map_reduce.rb', line 173
def reduced counts["reduce"] end
#results
Alias for #raw.
# 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.
# File 'lib/mongoid/contextual/map_reduce.rb', line 185
def scope(object) @map_reduce = @map_reduce.scope(object) self end
#time ⇒ Float
Get the execution time of the map/reduce.
# File 'lib/mongoid/contextual/map_reduce.rb', line 196
def time results["timeMillis"] end
#validate_out! (private)
# File 'lib/mongoid/contextual/map_reduce.rb', line 227
def validate_out! raise Errors::NoMapReduceOutput.new({}) unless @map_reduce.out end