class Mongo::Collection::View::Builder::Aggregation

Builds an aggregation command specification from the view and options.

@since 2.2.0

Constants

MAPPINGS

The mappings from ruby options to the aggregation options.

@since 2.2.0

Attributes

options[R]

@return [ Hash ] options The map/reduce specific options.

pipeline[R]

@return [ Array<Hash> ] pipeline The pipeline.

view[R]

@return [ Collection::View ] view The collection view.

Public Class Methods

new(pipeline, view, options) click to toggle source

Initialize the builder.

@example Initialize the builder.

Aggregation.new(map, reduce, view, options)

@param [ Array<Hash> ] pipeline The aggregation pipeline. @param [ Collection::View ] view The collection view. @param [ Hash ] options The map/reduce options.

@since 2.2.0

# File lib/mongo/collection/view/builder/aggregation.rb, line 58
def initialize(pipeline, view, options)
  @pipeline = pipeline
  @view = view
  @options = options
end

Public Instance Methods

specification() click to toggle source

Get the specification to pass to the aggregation operation.

@example Get the specification.

builder.specification

@return [ Hash ] The specification.

@since 2.2.0

# File lib/mongo/collection/view/builder/aggregation.rb, line 72
def specification
  spec = {
          selector: aggregation_command,
          db_name: database.name,
          read: read
         }
  write? ? spec.merge!(write_concern: write_concern) : spec
end

Private Instance Methods

aggregation_command() click to toggle source
# File lib/mongo/collection/view/builder/aggregation.rb, line 87
def aggregation_command
  command = BSON::Document.new(:aggregate => collection.name, :pipeline => pipeline)
  command[:cursor] = cursor if cursor
  command[:readConcern] = collection.read_concern if collection.read_concern
  command.merge!(Options::Mapper.transform_documents(options, MAPPINGS))
  command
end
batch_size_doc() click to toggle source
# File lib/mongo/collection/view/builder/aggregation.rb, line 101
def batch_size_doc
  (value = options[:batch_size] || view.batch_size) ?  { :batchSize => value } : {}
end
cursor() click to toggle source
# File lib/mongo/collection/view/builder/aggregation.rb, line 95
def cursor
  if options[:use_cursor] == true || options[:use_cursor].nil?
    batch_size_doc
  end
end
write?() click to toggle source
# File lib/mongo/collection/view/builder/aggregation.rb, line 83
def write?
  pipeline.any? { |operator| operator[:$out] || operator['$out'] }
end