class Mongo::Collection::View::Aggregation

Provides behaviour around an aggregation pipeline on a collection view.

@since 2.0.0

Constants

REROUTE

The reroute message.

@since 2.1.0

Attributes

pipeline[R]

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

view[R]

@return [ View ] view The collection view.

Public Class Methods

new(view, pipeline, options = {}) click to toggle source

Initialize the aggregation for the provided collection view, pipeline and options.

@example Create the new aggregation view.

Aggregation.view.new(view, pipeline)

@param [ Collection::View ] view The collection view. @param [ Array<Hash> ] pipeline The pipeline of operations. @param [ Hash ] options The aggregation options.

@since 2.0.0

# File lib/mongo/collection/view/aggregation.rb, line 73
def initialize(view, pipeline, options = {})
  @view = view
  @pipeline = pipeline.dup
  @options = options.dup
end

Public Instance Methods

allow_disk_use(value = nil) click to toggle source

Set to true if disk usage is allowed during the aggregation.

@example Set disk usage flag.

aggregation.allow_disk_use(true)

@param [ true, false ] value The flag value.

@return [ true, false, Aggregation ] The aggregation if a value was

set or the value if used as a getter.

@since 2.0.0

# File lib/mongo/collection/view/aggregation.rb, line 58
def allow_disk_use(value = nil)
  configure(:allow_disk_use, value)
end
explain() click to toggle source

Get the explain plan for the aggregation.

@example Get the explain plan for the aggregation.

aggregation.explain

@return [ Hash ] The explain plan.

@since 2.0.0

# File lib/mongo/collection/view/aggregation.rb, line 87
def explain
  self.class.new(view, pipeline, options.merge(explain: true)).first
end

Private Instance Methods

aggregate_spec() click to toggle source
# File lib/mongo/collection/view/aggregation.rb, line 93
def aggregate_spec
  Builder::Aggregation.new(pipeline, view, options).specification
end
initial_query_op() click to toggle source
# File lib/mongo/collection/view/aggregation.rb, line 101
def initial_query_op
  Operation::Commands::Aggregate.new(aggregate_spec)
end
new(options) click to toggle source
# File lib/mongo/collection/view/aggregation.rb, line 97
def new(options)
  Aggregation.new(view, pipeline, options)
end
secondary_ok?() click to toggle source
# File lib/mongo/collection/view/aggregation.rb, line 109
def secondary_ok?
  pipeline.none? { |op| op.key?('$out') || op.key?(:$out) }
end
send_initial_query(server) click to toggle source
# File lib/mongo/collection/view/aggregation.rb, line 113
def send_initial_query(server)
  unless valid_server?(server)
    log_warn(REROUTE)
    server = cluster.next_primary(false)
  end
  validate_collation!(server)
  initial_query_op.execute(server)
end
valid_server?(server) click to toggle source
# File lib/mongo/collection/view/aggregation.rb, line 105
def valid_server?(server)
  server.standalone? || server.mongos? || server.primary? || secondary_ok?
end
validate_collation!(server) click to toggle source
# File lib/mongo/collection/view/aggregation.rb, line 122
def validate_collation!(server)
  if (@options[:collation] || @options[Operation::COLLATION]) && !server.features.collation_enabled?
    raise Error::UnsupportedCollation.new
  end
end