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 63
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 77
def specification
  spec = {
          selector: aggregation_command,
          db_name: database.name,
          read: view.read_preference,
          session: @options[:session]
         }
  if write?
    spec.update(write_concern: write_concern)
  end
  spec
end

Private Instance Methods

aggregation_command() click to toggle source
# File lib/mongo/collection/view/builder/aggregation.rb, line 99
def aggregation_command
  command = BSON::Document.new
  # aggregate must be the first key in the command document
  if view.is_a?(Collection::View)
    command[:aggregate] = collection.name
  elsif view.is_a?(Database::View)
    command[:aggregate] = 1
  else
    raise ArgumentError, "Unknown view class: #{view}"
  end
  command[:pipeline] = pipeline
  if read_concern = view.read_concern
    command[:readConcern] = Options::Mapper.transform_values_to_strings(
      read_concern)
  end
  command[:cursor] = cursor if cursor
  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 125
def batch_size_doc
  value = options[:batch_size] || view.batch_size
  if value == 0 && write?
    {}
  elsif value
    { :batchSize => value }
  else
    {}
  end
end
cursor() click to toggle source
# File lib/mongo/collection/view/builder/aggregation.rb, line 119
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 92
def write?
  pipeline.any? do |operator|
    operator[:$out] || operator['$out'] ||
    operator[:$merge] || operator['$merge']
  end
end