Class: Mongoid::Contextual::MapReduce
- Inherits:
-
Object
- Object
- Mongoid::Contextual::MapReduce
- Extended by:
- Forwardable
- Includes:
- Enumerable, Command
- Defined in:
- build/mongoid-7.3/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
- #command ⇒ Object
-
#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.
-
#finalize(function) ⇒ MapReduce
Provide a finalize js function for the map/reduce.
-
#initialize(collection, criteria, map, reduce) ⇒ MapReduce
constructor
Initialize the new map/reduce directive.
-
#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.
-
#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.
Methods included from Command
Constructor Details
#initialize(collection, criteria, map, reduce) ⇒ MapReduce
Initialize the new map/reduce directive.
85 86 87 88 89 |
# File 'build/mongoid-7.3/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 |
Instance Method Details
#command ⇒ Object
246 247 248 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 246 def command @map_reduce.send(:map_reduce_spec)[:selector] end |
#counts ⇒ Hash
Get all the counts returned by the map/reduce.
22 23 24 |
# File 'build/mongoid-7.3/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.
37 38 39 40 41 42 43 44 45 46 |
# File 'build/mongoid-7.3/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 ⇒ Integer
Get the number of documents emitted by the map/reduce.
56 57 58 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 56 def emitted counts["emit"] end |
#finalize(function) ⇒ MapReduce
Provide a finalize js function for the map/reduce.
70 71 72 73 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 70 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.
99 100 101 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 99 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.
235 236 237 238 239 240 241 242 243 244 |
# File 'build/mongoid-7.3/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 ⇒ MapReduce
Sets the map/reduce to use jsMode.
111 112 113 114 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 111 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.
139 140 141 142 143 144 145 146 |
# File 'build/mongoid-7.3/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 ⇒ Integer
Get the number of documents output by the map/reduce.
156 157 158 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 156 def output counts["output"] end |
#raw ⇒ Hash Also known as: results, execute
Get the raw output from the map/reduce operation.
168 169 170 171 172 173 |
# File 'build/mongoid-7.3/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 |
#reduced ⇒ Integer
Get the number of documents reduced by the map/reduce.
195 196 197 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 195 def reduced counts["reduce"] end |
#scope(object) ⇒ MapReduce
Adds a javascript object to the global scope of the map/reduce.
209 210 211 212 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 209 def scope(object) @map_reduce = @map_reduce.scope(object) self end |
#time ⇒ Float
Get the execution time of the map/reduce.
222 223 224 |
# File 'build/mongoid-7.3/lib/mongoid/contextual/map_reduce.rb', line 222 def time results["timeMillis"] end |