Class: Mongo::Database::View

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Cursor::NonTailable, CursorHost, Retryable
Defined in:
lib/mongo/database/view.rb

Overview

A class representing a view of a database.

Since:

  • 2.0.0

Instance Attribute Summary collapse

Attributes included from CursorHost

#cursor, #timeout_mode

Instance Method Summary collapse

Methods included from Cursor::NonTailable

#cursor_type, #timeout_mode

Methods included from CursorHost

#validate_timeout_mode!

Methods included from Retryable

#read_worker, #select_server, #write_worker

Constructor Details

#initialize(database, options = {}) ⇒ View

Create the new database view.

Examples:

Create the new database view.

Database::View.new(database)

Parameters:

  • database (Database)

    The database.

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

    The options to configure the view with.

Options Hash (options):

  • :timeout_mode (:cursor_lifetime | :iteration)

    How to interpret :timeout_ms (whether it applies to the lifetime of the cursor, or per iteration).

  • :timeout_ms (Integer)

    The operation timeout in milliseconds. Must be a non-negative integer. An explicit value of 0 means infinite. The default value is unset which means the value is inherited from the database or the client.

Since:

  • 2.0.0



151
152
153
154
155
156
157
158
159
160
# File 'lib/mongo/database/view.rb', line 151

def initialize(database, options = {})
  @database = database
  @operation_timeout_ms = options.delete(:timeout_ms)

  validate_timeout_mode!(options)

  @batch_size =  nil
  @limit = nil
  @collection = @database[Database::COMMAND]
end

Instance Attribute Details

#batch_sizeInteger (readonly)

Returns batch_size The size of the batch of results when sending the listCollections command.

Returns:

  • (Integer)

    batch_size The size of the batch of results when sending the listCollections command.

Since:

  • 2.0.0



40
41
42
# File 'lib/mongo/database/view.rb', line 40

def batch_size
  @batch_size
end

#collectionCollection (readonly)

Returns collection The command collection.

Returns:

  • (Collection)

    collection The command collection.

Since:

  • 2.0.0



46
47
48
# File 'lib/mongo/database/view.rb', line 46

def collection
  @collection
end

#databaseObject (readonly)

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.

Since:

  • 2.0.0



163
164
165
# File 'lib/mongo/database/view.rb', line 163

def database
  @database
end

#limitInteger (readonly)

Returns limit The limit when sending a command.

Returns:

  • (Integer)

    limit The limit when sending a command.

Since:

  • 2.0.0



43
44
45
# File 'lib/mongo/database/view.rb', line 43

def limit
  @limit
end

#operation_timeout_msInteger | nil | The timeout_ms value that was passed as an option to the view. (readonly)

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.

Returns Integer | nil | The timeout_ms value that was passed as an option to the view.

Returns:

  • (Integer | nil | The timeout_ms value that was passed as an option to the view.)

    Integer | nil | The timeout_ms value that was passed as an option to the view.

Since:

  • 2.0.0



169
170
171
# File 'lib/mongo/database/view.rb', line 169

def operation_timeout_ms
  @operation_timeout_ms
end

Instance Method Details

#aggregate(pipeline, options = {}) ⇒ Collection::View::Aggregation

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.

Execute an aggregation on the database view.

Examples:

Aggregate documents.

view.aggregate([
  { "$listLocalSessions" => {} }
])

Parameters:

  • pipeline (Array<Hash>)

    The aggregation pipeline.

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

    The aggregation options.

Returns:

Since:

  • 2.10.0



185
186
187
# File 'lib/mongo/database/view.rb', line 185

def aggregate(pipeline, options = {})
  Collection::View::Aggregation.new(self, pipeline, options)
end

#collection_names(options = {}) ⇒ Array<String>

Note:

The set of returned collection names depends on the version of MongoDB server that fulfills the request.

Get all the names of the non-system collections in the database.

See https://mongodb.com/docs/manual/reference/command/listCollections/
for more information and usage.

Parameters:

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

    Options for the listCollections command.

Options Hash (options):

  • :batch_size (Integer)

    The batch size for results returned from the listCollections command.

  • :filter (Hash)

    A filter on the collections returned.

  • :authorized_collections (true, false)

    A flag, when set to true, that allows a user without the required privilege to run the command when access control is enforced.

  • :comment (Object)

    A user-provided comment to attach to this command.

  • :timeout_ms (Integer)

    The operation timeout in milliseconds. Must be a non-negative integer. An explicit value of 0 means infinite. The default value is unset which means the value is inherited from the database or the client.

  • :session (Session)

    The session to use.

Returns:

  • (Array<String>)

    The names of all non-system collections.

Since:

  • 2.0.0



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mongo/database/view.rb', line 77

def collection_names(options = {})
  @batch_size = options[:batch_size]
  session = client.get_session(options)
  context = Operation::Context.new(
    client: client,
    session: session,
    operation_timeouts: operation_timeouts(options)
  )
  cursor = read_with_retry_cursor(session, ServerSelector.primary, self, context: context) do |server|
    send_initial_query(server, session, context, options.merge(name_only: true))
  end
  cursor.map do |info|
    if cursor.initial_result.connection_description.features.list_collections_enabled?
      info['name']
    else
      (info['name'] &&
        info['name'].sub("#{@database.name}.", ''))
    end
  end.reject do |name|
    name.start_with?('system.') || name.include?('$')
  end
end

#list_collections(options = {}) ⇒ Array<Hash>

Note:

The set of collections returned, and the schema of the information hash per collection, depends on the MongoDB server version that fulfills the request.

Get info on all the collections in the database.

Examples:

Get info on each collection.

database.list_collections

Parameters:

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

Options Hash (options):

  • :filter (Hash)

    A filter on the collections returned.

  • :name_only (true, false)

    Indicates whether command should return just collection/view names and type or return both the name and other information

  • :authorized_collections (true, false)

    A flag, when set to true and used with nameOnly: true, that allows a user without the required privilege to run the command when access control is enforced

    See mongodb.com/docs/manual/reference/command/listCollections/ for more information and usage.

  • :session (Session)

    The session to use.

  • :deserialize_as_bson (Boolean)

    Whether to deserialize this message using BSON types instead of native Ruby types wherever possible.

Returns:

  • (Array<Hash>)

    Info for each collection in the database.

Since:

  • 2.0.5



129
130
131
132
# File 'lib/mongo/database/view.rb', line 129

def list_collections(options = {})
  session = client.get_session(options)
  collections_info(session, ServerSelector.primary, options)
end

#operation_timeouts(opts = {}) ⇒ Hash

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.

Returns timeout_ms value set on the operation level (if any).

Returns:

  • (Hash)

    timeout_ms value set on the operation level (if any).

Since:

  • 2.0.0



200
201
202
203
204
205
206
207
208
# File 'lib/mongo/database/view.rb', line 200

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

#timeout_msInteger | nil

The timeout_ms value to use for this operation; either specified as an option to the view, or inherited from the database.

Returns:

  • (Integer | nil)

    the timeout_ms for this operation

Since:

  • 2.0.0



193
194
195
# File 'lib/mongo/database/view.rb', line 193

def timeout_ms
  operation_timeout_ms || database.timeout_ms
end