Class: Mongo::Database::View

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, 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

Instance Method Summary collapse

Methods included from Retryable

#legacy_write_with_retry, #nro_write_with_retry, #read_with_one_retry, #read_with_retry, #read_with_retry_cursor, #write_with_retry

Constructor Details

#initialize(database) ⇒ View

Create the new database view.

Examples:

Create the new database view.

View::Index.new(database)

Parameters:

Since:

  • 2.0.0



103
104
105
106
107
108
# File 'lib/mongo/database/view.rb', line 103

def initialize(database)
  @database = database
  @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



33
34
35
# File 'lib/mongo/database/view.rb', line 33

def batch_size
  @batch_size
end

#collectionCollection (readonly)

Returns collection The command collection.

Returns:

  • (Collection)

    collection The command collection.

Since:

  • 2.0.0



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

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



111
112
113
# File 'lib/mongo/database/view.rb', line 111

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



36
37
38
# File 'lib/mongo/database/view.rb', line 36

def limit
  @limit
end

Instance Method Details

#aggregate(pipeline, options = {}) ⇒ 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:

  • (Aggregation)

    The aggregation object.

Since:

  • 2.10.0



127
128
129
# File 'lib/mongo/database/view.rb', line 127

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.

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.

Returns:

  • (Array<String>)

    The names of all non-system collections.

Since:

  • 2.0.0



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mongo/database/view.rb', line 54

def collection_names(options = {})
  @batch_size = options[:batch_size]
  session = client.send(:get_session, options)
  cursor = read_with_retry_cursor(session, ServerSelector.primary, self) do |server|
    send_initial_query(server, session, name_only: true)
  end
  cursor.map do |info|
    if cursor.server.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)

Options Hash (**options):

Returns:

  • (Array<Hash>)

    Info for each collection in the database.

Since:

  • 2.0.5



90
91
92
93
# File 'lib/mongo/database/view.rb', line 90

def list_collections(**options)
  session = client.send(:get_session)
  collections_info(session, ServerSelector.primary, **options)
end