Class: Mongo::Database::View

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

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



85
86
87
88
89
90
# File 'lib/mongo/database/view.rb', line 85

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



30
31
32
# File 'lib/mongo/database/view.rb', line 30

def batch_size
  @batch_size
end

#collectionCollection (readonly)

Returns collection The command collection.

Returns:

  • (Collection)

    collection The command collection.

Since:

  • 2.0.0



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

def collection
  @collection
end

#limitInteger (readonly)

Returns limit The limit when sending a command.

Returns:

  • (Integer)

    limit The limit when sending a command.

Since:

  • 2.0.0



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

def limit
  @limit
end

Instance Method Details

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

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

Examples:

Get the collection names.

database.collection_names

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



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mongo/database/view.rb', line 51

def collection_names(options = {})
  @batch_size = options[:batch_size]
  server = next_primary(false)
  @limit = -1 if server.features.list_collections_enabled?
  collections_info(server).collect do |info|
    if server.features.list_collections_enabled?
      info[Database::NAME]
    else
      (info[Database::NAME] &&
        info[Database::NAME].sub("#{@database.name}.", ''))
    end
  end
end

#list_collectionsArray<Hash>

Get info on all the collections in the database.

Examples:

Get info on each collection.

database.list_collections

Returns:

  • (Array<Hash>)

    Info for each collection in the database.

Since:

  • 2.0.5



73
74
75
# File 'lib/mongo/database/view.rb', line 73

def list_collections
  collections_info(next_primary(false))
end