123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Collection::View::Builder::Aggregation

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongo/collection/view/builder/aggregation.rb

Overview

Builds an aggregation command specification from the view and options.

Since:

  • 2.2.0

Constant Summary

  • MAPPINGS =

    The mappings from ruby options to the aggregation options.

    Since:

    • 2.2.0

    # File 'lib/mongo/collection/view/builder/aggregation.rb', line 32
    BSON::Document.new(
      allow_disk_use: 'allowDiskUse',
      bypass_document_validation: 'bypassDocumentValidation',
      explain: 'explain',
      collation: 'collation',
      comment: 'comment',
      hint: 'hint',
      let: 'let',
      # This is intentional; max_await_time_ms is an alias for maxTimeMS
      # used on getMore commands for change streams.
      max_await_time_ms: 'maxTimeMS',
      max_time_ms: 'maxTimeMS',
    ).freeze

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(pipeline, view, options) ⇒ Aggregation

Initialize the builder.

Parameters:

  • pipeline (Array<Hash>)

    The aggregation pipeline.

  • view (Collection::View)

    The collection view.

  • options (Hash)

    The map/reduce and read preference options.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 64

def initialize(pipeline, view, options)
  @pipeline = pipeline
  @view = view
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)

    options The map/reduce specific options.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 55

attr_reader :options

#pipelineArray<Hash> (readonly)

Returns:

  • (Array<Hash>)

    pipeline The pipeline.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 49

attr_reader :pipeline

#viewCollection::View (readonly)

Returns:

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 52

attr_reader :view

#write?Boolean (readonly, private)

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 94

def write?
  pipeline.any? do |operator|
    operator[:$out] || operator['$out'] ||
    operator[:$merge] || operator['$merge']
  end
end

Instance Method Details

#aggregation_command (private)

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 101

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 (private)

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 127

def batch_size_doc
  value = options[:batch_size] || view.batch_size
  if value == 0 && write?
    {}
  elsif value
    { :batchSize => value }
  else
    {}
  end
end

#cursor (private)

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 121

def cursor
  if options[:use_cursor] == true || options[:use_cursor].nil?
    batch_size_doc
  end
end

#specificationHash

Get the specification to pass to the aggregation operation.

Examples:

Get the specification.

builder.specification

Returns:

  • (Hash)

    The specification.

Since:

  • 2.2.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/builder/aggregation.rb', line 78

def specification
  spec = {
    selector: aggregation_command,
    db_name: database.name,
    read: @options[:read_preference] || view.read_preference,
    session: @options[:session],
    collation: @options[:collation],
  }
  if write?
    spec.update(write_concern: write_concern)
  end
  spec
end