Class: Mongo::Collection::View

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Explainable, Immutable, Iterable, Readable, Writable, Retryable
Defined in:
lib/mongo/collection/view.rb,
lib/mongo/collection/view/iterable.rb,
lib/mongo/collection/view/readable.rb,
lib/mongo/collection/view/writable.rb,
lib/mongo/collection/view/immutable.rb,
lib/mongo/collection/view/map_reduce.rb,
lib/mongo/collection/view/aggregation.rb,
lib/mongo/collection/view/explainable.rb,
lib/mongo/collection/view/builder/flags.rb,
lib/mongo/collection/view/builder/op_query.rb,
lib/mongo/collection/view/builder/modifiers.rb,
lib/mongo/collection/view/builder/map_reduce.rb,
lib/mongo/collection/view/builder/aggregation.rb,
lib/mongo/collection/view/builder/find_command.rb

Overview

Note:

The View API is semipublic.

Representation of a query and options producing a result set of documents.

A View can be modified using helpers. Helpers can be chained, as each one returns a View if arguments are provided.

The query message is sent to the server when a “terminator” is called. For example, when #each is called on a View, a Cursor object is created, which then sends the query to the server.

A View is not created directly by a user. Rather, View creates a View when a CRUD operation is called and returns it to the user to interact with.

Since:

  • 2.0.0

Defined Under Namespace

Modules: Builder, Explainable, Immutable, Iterable, Readable, Writable Classes: Aggregation, MapReduce

Constant Summary

Constants included from Explainable

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

Constants included from Readable

Readable::MODIFIERS, Readable::QUERY

Instance Attribute Summary collapse

Attributes included from Immutable

#options

Instance Method Summary collapse

Methods included from Writable

#delete_many, #delete_one, #find_one_and_delete, #find_one_and_replace, #find_one_and_update, #replace_one, #update_many, #update_one

Methods included from Explainable

#explain

Methods included from Retryable

#read_with_one_retry, #read_with_retry, #write_with_retry

Methods included from Readable

#aggregate, #allow_partial_results, #await_data, #batch_size, #comment, #count, #cursor_type, #distinct, #hint, #limit, #map_reduce, #max_await_time_ms, #max_scan, #max_time_ms, #max_value, #min_value, #modifiers, #no_cursor_timeout, #projection, #read, #return_key, #show_disk_loc, #skip, #snapshot, #sort

Methods included from Iterable

#close_query, #each

Constructor Details

#initialize(collection, filter = {}, options = {}) ⇒ View

Creates a new View.

Examples:

Find all users named Emily.

View.new(collection, {:name => 'Emily'})

Find all users named Emily skipping 5 and returning 10.

View.new(collection, {:name => 'Emily'}, :skip => 5, :limit => 10)

Find all users named Emily using a specific read preference.

View.new(collection, {:name => 'Emily'}, :read => :secondary_preferred)

Parameters:

  • collection (Collection)

    The Collection to query.

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

    The query filter.

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

    The additional query options.

Options Hash (options):

  • :comment (String)

    Associate a comment with the query.

  • :batch_size (Integer)

    The number of docs to return in each response from MongoDB.

  • :fields (Hash)

    The fields to include or exclude in returned docs.

  • :hint (Hash)

    Override default index selection and force MongoDB to use a specific index for the query.

  • :limit (Integer)

    Max number of docs to return.

  • :max_scan (Integer)

    Constrain the query to only scan the specified number of docs. Use to prevent queries from running too long.

  • :read (Symbol)

    The read preference to use for the query. If none is provided, the collection's default read preference is used.

  • :show_disk_loc (true, false)

    Return disk location info as a field in each doc.

  • :skip (Integer)

    The number of documents to skip.

  • :snapshot (true, false)

    Prevents returning a doc more than once.

  • :sort (Hash)

    The key and direction pairs used to sort the results.

  • :collation (Hash)

    The collation to use.

Since:

  • 2.0.0



137
138
139
140
141
# File 'lib/mongo/collection/view.rb', line 137

def initialize(collection, filter = {}, options = {})
  validate_doc!(filter)
  @collection = collection
  parse_parameters!(BSON::Document.new(filter), BSON::Document.new(options))
end

Instance Attribute Details

#collectionCollection (readonly)

Returns The Collection to query.

Returns:

Since:

  • 2.0.0



53
54
55
# File 'lib/mongo/collection/view.rb', line 53

def collection
  @collection
end

#filterHash (readonly) Also known as: selector

Returns The query filter.

Returns:

  • (Hash)

    The query filter.

Since:

  • 2.0.0



56
57
58
# File 'lib/mongo/collection/view.rb', line 56

def filter
  @filter
end

Instance Method Details

#==(other) ⇒ true, false Also known as: eql?

Compare two View objects.

Examples:

Compare the view with another object.

view == other

Returns:

  • (true, false)

    Equal if collection, filter, and options of two View match.

Since:

  • 2.0.0



79
80
81
82
83
84
# File 'lib/mongo/collection/view.rb', line 79

def ==(other)
  return false unless other.is_a?(View)
  collection == other.collection &&
      filter == other.filter &&
      options == other.options
end

#hashInteger

A hash value for the View composed of the collection namespace, hash of the options and hash of the filter.

Examples:

Get the hash value.

view.hash

Returns:

  • (Integer)

    A hash value of the View object.

Since:

  • 2.0.0



96
97
98
# File 'lib/mongo/collection/view.rb', line 96

def hash
  [ collection.namespace, options.hash, filter.hash ].hash
end

#inspectString

Get a human-readable string representation of View.

Examples:

Get the inspection.

view.inspect

Returns:

  • (String)

    A string representation of a View instance.

Since:

  • 2.0.0



151
152
153
154
# File 'lib/mongo/collection/view.rb', line 151

def inspect
  "#<Mongo::Collection::View:0x#{object_id} namespace='#{collection.namespace}'" +
      " @filter=#{filter.to_s} @options=#{options.to_s}>"
end

#write_concernMongo::WriteConcern

Get the write concern on this View.

Examples:

Get the write concern.

view.write_concern

Returns:

Since:

  • 2.0.0



164
165
166
# File 'lib/mongo/collection/view.rb', line 164

def write_concern
  WriteConcern.get(options[:write] || options[:write_concern] || collection.write_concern)
end