class Mongo::Collection::View

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.

@note The View API is semipublic. @api semipublic

Attributes

collection[R]

@return [ Collection ] The Collection to query.

filter[R]

@return [ Hash ] The query filter.

selector[R]

@return [ Hash ] The query filter.

Public Class Methods

new(collection, filter = {}, options = {}) click to toggle source

Creates a new View.

@example Find all users named Emily.

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

@example Find all users named Emily skipping 5 and returning 10.

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

@example Find all users named Emily using a specific read preference.

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

@param [ Collection ] collection The Collection to query. @param [ Hash ] filter The query filter. @param [ Hash ] options The additional query options.

@option options [ true, false ] :allow_disk_use When set to true, the

server can write temporary data to disk while executing the find
operation. This option is only available on MongoDB server versions
4.4 and newer.

@option options [ Integer ] :batch_size The number of documents to

return in each response from MongoDB.

@option options [ Hash ] :collation The collation to use. @option options [ String ] :comment Associate a comment with the query. @option options [ Hash ] :explain Execute an explain with the provided

explain options (known options are :verbose and :verbosity) rather
than a find.

@option options [ Hash ] :hint Override the default index selection and

force MongoDB to use a specific index for the query.

@option options [ Integer ] :limit Max number of documents to return. @option options [ Integer ] :max_scan Constrain the query to only scan

the specified number of documents. Use to prevent queries from
running for too long. Deprecated as of MongoDB server version 4.0.

@option options [ Hash ] :projection The fields to include or exclude

in the returned documents.

@option options [ Hash ] :read The read preference to use for the

query. If none is provided, the collection's default read preference
is used.

@option options [ Hash ] :read_concern The read concern to use for

the query.

@option options [ true | false ] :show_disk_loc Return disk location

info as a field in each doc.

@option options [ Integer ] :skip The number of documents to skip. @option options [ true | false ] :snapshot Prevents returning a

document more than once. Deprecated as of MongoDB server version 4.0.

@option options [ Hash ] :sort The key and direction pairs used to sort

the results.

@since 2.0.0

# File lib/mongo/collection/view.rb, line 155
def initialize(collection, filter = {}, options = {})
  validate_doc!(filter)
  @collection = collection

  filter = BSON::Document.new(filter)
  options = BSON::Document.new(options)

  # This is when users pass $query in filter and other modifiers
  # alongside?
  query = filter.delete(:$query)
  # This makes modifiers contain the filter if filter wasn't
  # given via $query but as top-level keys, presumably
  # downstream code ignores non-modifier keys in the modifiers?
  modifiers = filter.merge(options.delete(:modifiers) || {})
  @filter = (query || filter).freeze
  @options = Operation::Find::Builder::Modifiers.map_driver_options(modifiers).merge!(options).freeze
end

Public Instance Methods

==(other) click to toggle source

Compare two View objects.

@example Compare the view with another object.

view == other

@return [ true, false ] Equal if collection, filter, and options of two

+View+ match.

@since 2.0.0

# File lib/mongo/collection/view.rb, line 86
def ==(other)
  return false unless other.is_a?(View)
  collection == other.collection &&
      filter == other.filter &&
      options == other.options
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source

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

@example Get the hash value.

view.hash

@return [ Integer ] A hash value of the View object.

@since 2.0.0

# File lib/mongo/collection/view.rb, line 103
def hash
  [ collection.namespace, options.hash, filter.hash ].hash
end
inspect() click to toggle source

Get a human-readable string representation of View.

@example Get the inspection.

view.inspect

@return [ String ] A string representation of a View instance.

@since 2.0.0

# File lib/mongo/collection/view.rb, line 181
def inspect
  "#<Mongo::Collection::View:0x#{object_id} namespace='#{collection.namespace}'" +
      " @filter=#{filter.to_s} @options=#{options.to_s}>"
end
write_concern() click to toggle source

Get the write concern on this View.

@example Get the write concern.

view.write_concern

@return [ Mongo::WriteConcern ] The write concern.

@since 2.0.0

# File lib/mongo/collection/view.rb, line 194
def write_concern
  WriteConcern.get(options[:write_concern] || options[:write] || collection.write_concern)
end

Private Instance Methods

initialize_copy(other) click to toggle source
# File lib/mongo/collection/view.rb, line 200
def initialize_copy(other)
  @collection = other.collection
  @options = other.options.dup
  @filter = other.filter.dup
end
new(options) click to toggle source
# File lib/mongo/collection/view.rb, line 206
def new(options)
  View.new(collection, filter, options)
end
view() click to toggle source
# File lib/mongo/collection/view.rb, line 210
def view; self; end
with_session(opts = {}, &block) click to toggle source
# File lib/mongo/collection/view.rb, line 212
def with_session(opts = {}, &block)
  client.send(:with_session, @options.merge(opts), &block)
end