123456789_123456789_123456789_123456789_123456789_

Module: Mongo::Collection::View::Aggregation::Behavior

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Instance Chain:
Defined in: lib/mongo/collection/view/aggregation/behavior.rb

Overview

Distills the behavior common to aggregator classes, like View::Aggregator and ::Mongo::Collection::View::ChangeStream.

Since:

  • 2.0.0

Constant Summary

::Mongo::Collection::View::Explainable - Included

ALL_PLANS_EXECUTION, EXECUTION_STATS, QUERY_PLANNER

::Mongo::Loggable - Included

PREFIX

Instance Attribute Summary

::Mongo::Collection::View::Explainable - Included

::Mongo::Collection::View::Iterable - Included

#prefer_cached_cursor?

If the caching cursor is closed and was not fully iterated, the documents we have in it are not the complete result set and we have no way of completing that iteration.

#use_query_cache?

::Mongo::CursorHost - Included

#cursor

Returns the cursor associated with this view, if any.

#timeout_mode

::Mongo::Collection::View::Immutable - Included

Instance Method Summary

::Mongo::Retryable - Included

#read_worker

Returns the read worker for handling retryable reads.

#select_server

This is a separate method to make it possible for the test suite to assert that server selection is performed during retry attempts.

#write_worker

Returns the write worker for handling retryable writes.

::Mongo::Loggable - Included

#log_debug

Convenience method to log debug messages with the standard prefix.

#log_error

Convenience method to log error messages with the standard prefix.

#log_fatal

Convenience method to log fatal messages with the standard prefix.

#log_info

Convenience method to log info messages with the standard prefix.

#log_warn

Convenience method to log warn messages with the standard prefix.

#logger

Get the logger instance.

#_mongo_log_prefix, #format_message

::Mongo::Collection::View::Explainable - Included

#explain

Get the query plan for the query.

#explain_options

::Mongo::Collection::View::Iterable - Included

#close_query

Cleans up resources associated with this query.

#each

Iterate through documents returned by a query with this ::Mongo::Collection::View.

#kill_cursors
#cache_options, #cached_cursor, #compute_limit_for_cached_query, #initial_query_op,
#maybe_set_tailable_options

Add tailable cusror options to the command specifiction if needed.

#new_cursor_for_iteration

Start a new cursor for use when iterating (via #each).

#oplog_replay, #select_cursor, #send_initial_query

::Mongo::CursorHost - Included

#validate_timeout_mode!

Ensure the timeout mode is appropriate for other options that have been given.

::Mongo::Collection::View::Immutable - Included

Instance Attribute Details

#viewView (readonly)

Returns:

  • (View)

    view The collection view.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 19

attr_reader :view

#write?Boolean (readonly)

This method is for internal use only.

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

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 60

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

Instance Method Details

#aggregate_spec(session, read_preference) (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 91

def aggregate_spec(session, read_preference)
  Builder::Aggregation.new(
    pipeline,
    view,
    options.merge(session: session, read_preference: read_preference)
  ).specification
end

#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

[ GitHub ]

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

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

#cache_options (private)

Skip, sort, limit, projection are specified as pipeline stages rather than as options.

Since:

  • 2.0.0

[ GitHub ]

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

def cache_options
  {
    namespace: collection.namespace,
    selector: pipeline,
    read_concern: view.read_concern,
    read_preference: view.read_preference,
    collation: options[:collation],
    # Aggregations can read documents from more than one collection,
    # so they will be cleared on every write operation.
    multi_collection: true,
  }
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

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 50

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

#operation_timeouts(opts = {}) ⇒ Hash (private)

This method is for internal use only.

Returns:

  • (Hash)

    timeout_ms value set on the operation level (if any), and/or timeout_ms that is set on collection/database/client level (if any).

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 118

def operation_timeouts(opts = {})
  {}.tap do |result|
    if opts[:timeout_ms] || @timeout_ms
      result[:operation_timeout_ms] = opts.delete(:timeout_ms) || @timeout_ms
    else
      result[:inherited_timeout_ms] = view.timeout_ms
    end
  end
end

#perform_setup(view, options, forbid: []) (private)

Common setup for all classes that include this behavior; the constructor should invoke this method.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 76

def perform_setup(view, options, forbid: [])
  @view = view

  @timeout_ms = options.delete(:timeout_ms)
  @options = BSON::Document.new(options).freeze

  yield

  validate_timeout_mode!(options, forbid: forbid)
end

#server_selector (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 87

def server_selector
  @view.send(:server_selector)
end

#timeout_msInteger | nil

This method is for internal use only.

Returns:

  • (Integer | nil)

    the timeout_ms value that was passed as an option to this object, or which was inherited from the view.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/collection/view/aggregation/behavior.rb', line 68

def timeout_ms
  @timeout_ms || view.timeout_ms
end