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

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Explainable, Immutable, Iterable, Loggable, Retryable
Defined in:
lib/mongo/collection/view/aggregation.rb

Overview

Provides behavior around an aggregation pipeline on a collection view.

Since:

  • 2.0.0

Direct Known Subclasses

ChangeStream

Constant Summary collapse

REROUTE =
Deprecated.

The reroute message.

Since:

  • 2.1.0

'Rerouting the Aggregation operation to the primary server.'.freeze

Constants included from Loggable

Loggable::PREFIX

Constants included from Explainable

Explainable::ALL_PLANS_EXECUTION, Explainable::EXECUTION_STATS, Explainable::QUERY_PLANNER

Instance Attribute Summary collapse

Attributes included from Iterable

#cursor

Attributes included from Immutable

#options

Instance Method Summary collapse

Methods included from Retryable

#read_worker, #select_server, #write_worker

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Methods included from Iterable

#close_query, #each

Constructor Details

#initialize(view, pipeline, options = {}) ⇒ Aggregation

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

Examples:

Create the new aggregation view.

Aggregation.view.new(view, pipeline)

Parameters:

  • view (Collection::View)

    The collection view.

  • pipeline (Array<Hash>)

    The pipeline of operations.

  • options (Hash) (defaults to: {})

    The aggregation options.

Options Hash (options):

  • :allow_disk_use (true, false)

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

  • :batch_size (Integer)

    The number of documents to return per batch.

  • :bypass_document_validation (true, false)

    Whether or not to skip document level validation.

  • :collation (Hash)

    The collation to use.

  • :comment (Object)

    A user-provided comment to attach to this command.

  • :hint (String)

    The index to use for the aggregation.

  • :let (Hash)

    Mapping of variables to use in the pipeline. See the server documentation for details.

  • :max_time_ms (Integer)

    The maximum amount of time in milliseconds to allow the aggregation to run.

  • :use_cursor (true, false)

    Indicates whether the command will request that the server provide results using a cursor. Note that as of server version 3.6, aggregations always provide results using a cursor and this option is therefore not valid.

  • :session (Session)

    The session to use.

Since:

  • 2.0.0



97
98
99
100
101
102
103
104
# File 'lib/mongo/collection/view/aggregation.rb', line 97

def initialize(view, pipeline, options = {})
  @view = view
  @pipeline = pipeline.dup
  unless Mongo.broken_view_aggregate || view.filter.empty?
    @pipeline.unshift(:$match => view.filter)
  end
  @options = BSON::Document.new(options).freeze
end

Instance Attribute Details

#pipelineArray<Hash> (readonly)

Returns pipeline The aggregation pipeline.

Returns:

  • (Array<Hash>)

    pipeline The aggregation pipeline.

Since:

  • 2.0.0



37
38
39
# File 'lib/mongo/collection/view/aggregation.rb', line 37

def pipeline
  @pipeline
end

#viewView (readonly)

Returns view The collection view.

Returns:

  • (View)

    view The collection view.

Since:

  • 2.0.0



35
36
37
# File 'lib/mongo/collection/view/aggregation.rb', line 35

def view
  @view
end

Instance Method Details

#allow_disk_use(value = nil) ⇒ true, ...

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

Examples:

Set disk usage flag.

aggregation.allow_disk_use(true)

Parameters:

  • value (true, false) (defaults to: nil)

    The flag value.

Returns:

  • (true, false, Aggregation)

    The aggregation if a value was set or the value if used as a getter.

Since:

  • 2.0.0



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

def allow_disk_use(value = nil)
  configure(:allow_disk_use, value)
end

#explainHash

Get the explain plan for the aggregation.

Examples:

Get the explain plan for the aggregation.

aggregation.explain

Returns:

  • (Hash)

    The explain plan.

Since:

  • 2.0.0



114
115
116
# File 'lib/mongo/collection/view/aggregation.rb', line 114

def explain
  self.class.new(view, pipeline, options.merge(explain: true)).first
end

#write?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether this aggregation will write its result to a database collection.

Returns:

  • (Boolean)

    Whether the aggregation will write its result to a collection.

Since:

  • 2.0.0



124
125
126
# File 'lib/mongo/collection/view/aggregation.rb', line 124

def write?
  pipeline.any? { |op| op.key?('$out') || op.key?(:$out) || op.key?('$merge') || op.key?(:$merge) }
end