module Mongo::Collection::View::Explainable

Defines explain related behavior for collection view.

@since 2.0.0

Constants

ALL_PLANS_EXECUTION

The all plans execution verbosity constant.

@since 2.2.0

EXECUTION_STATS

The execution stats verbosity constant.

@since 2.2.0

QUERY_PLANNER

The query planner verbosity constant.

@since 2.2.0

Public Instance Methods

explain(**opts) click to toggle source

Get the query plan for the query.

@example Get the query plan for the query with execution statistics.

view.explain(verbosity: :execution_stats)

@option opts [ true | false ] :verbose The level of detail

to return for MongoDB 2.6 servers.

@option opts [ String | Symbol ] :verbosity The type of information

to return for MongoDB 3.0 and newer servers. If the value is a
symbol, it will be stringified and converted from underscore
style to camel case style (e.g. :query_planner => "queryPlanner").

@return [ Hash ] A single document with the query plan.

@see docs.mongodb.com/manual/reference/method/db.collection.explain/#db.collection.explain

@since 2.0.0

# File lib/mongo/collection/view/explainable.rb, line 56
def explain(**opts)
  self.class.new(collection, selector, options.merge(explain_options(**opts))).first
end

Private Instance Methods

explain_options(**opts) click to toggle source

@option opts [ true | false ] :verbose The level of detail

to return for MongoDB 2.6 servers.

@option opts [ String | Symbol ] :verbosity The type of information

to return for MongoDB 3.0 and newer servers. If the value is a
symbol, it will be stringified and converted from underscore
style to camel case style (e.g. :query_planner => "queryPlanner").
# File lib/mongo/collection/view/explainable.rb, line 72
def explain_options(**opts)
  explain_limit = limit || 0
  # Note: opts will never be nil here.
  if Symbol === opts[:verbosity]
    opts[:verbosity] = Utils.camelize(opts[:verbosity])
  end
  { limit: -explain_limit.abs, explain: opts }
end
explained?() click to toggle source
# File lib/mongo/collection/view/explainable.rb, line 62
def explained?
  !!options[:explain]
end