class Mongo::Database::View

A class representing a view of a database.

@since 2.0.0

Attributes

batch_size[R]

@return [ Integer ] batch_size The size of the batch of results

when sending the listCollections command.
collection[R]

@return [ Collection ] collection The command collection.

limit[R]

@return [ Integer ] limit The limit when sending a command.

Public Class Methods

new(database) click to toggle source

Create the new database view.

@example Create the new database view.

View::Index.new(database)

@param [ Database ] database The database.

@since 2.0.0

# File lib/mongo/database/view.rb, line 89
def initialize(database)
  @database = database
  @batch_size =  nil
  @limit = nil
  @collection = @database[Database::COMMAND]
end

Public Instance Methods

collection_names(options = {}) click to toggle source

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

@example Get the collection names.

database.collection_names

@param [ Hash ] options Options for the listCollections command.

@option options [ Integer ] :batch_size The batch size for results

returned from the listCollections command.

@return [ Array<String> ] The names of all non-system collections.

@since 2.0.0

# File lib/mongo/database/view.rb, line 52
def collection_names(options = {})
  @batch_size = options[:batch_size]
  session = client.send(:get_session, options)
  cursor = read_with_retry_cursor(session, ServerSelector.get(mode: :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[Database::NAME]
    else
      (info[Database::NAME] &&
        info[Database::NAME].sub("#{@database.name}.", ''))
    end
  end
end
list_collections() click to toggle source

Get info on all the collections in the database.

@example Get info on each collection.

database.list_collections

@return [ Array<Hash> ] Info for each collection in the database.

@since 2.0.5

# File lib/mongo/database/view.rb, line 76
def list_collections
  session = client.send(:get_session)
  collections_info(session, ServerSelector.get(mode: :primary))
end

Private Instance Methods

collections_info(session, server_selector, options = {}) { |doc| ... } click to toggle source
# File lib/mongo/database/view.rb, line 98
def collections_info(session, server_selector, options = {}, &block)
  cursor = read_with_retry_cursor(session, server_selector, self) do |server|
    send_initial_query(server, session, options)
  end
  if block_given?
    cursor.each do |doc|
      yield doc
    end
  else
    cursor.to_enum
  end
end
collections_info_spec(session, options = {}) click to toggle source
# File lib/mongo/database/view.rb, line 111
def collections_info_spec(session, options = {})
  { selector: {
      listCollections: 1,
      cursor: batch_size ? { batchSize: batch_size } : {} },
    db_name: @database.name,
    session: session
  }.tap { |spec| spec[:selector][:nameOnly] = true if options[:name_only] }
end
initial_query_op(session, options = {}) click to toggle source
# File lib/mongo/database/view.rb, line 120
def initial_query_op(session, options = {})
  Operation::CollectionsInfo.new(collections_info_spec(session, options))
end
send_initial_query(server, session, options = {}) click to toggle source
# File lib/mongo/database/view.rb, line 124
def send_initial_query(server, session, options = {})
  initial_query_op(session, options).execute(server)
end