Class: Mongo::Index::View

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/mongo/index/view.rb

Overview

A class representing a view of indexes.

Since:

  • 2.0.0

Constant Summary

KEY =

The index key field.

Since:

  • 2.0.0

'key'.freeze
NAME =

The index name field.

Since:

  • 2.0.0

'name'.freeze
OPTIONS =

The mappings of Ruby index options to server options.

Since:

  • 2.0.0

{
  :background => :background,
  :bits => :bits,
  :bucket_size => :bucketSize,
  :default_language => :default_language,
  :expire_after => :expireAfterSeconds,
  :expire_after_seconds => :expireAfterSeconds,
  :key => :key,
  :language_override => :language_override,
  :max => :max,
  :min => :min,
  :name => :name,
  :partial_filter_expression => :partialFilterExpression,
  :sparse => :sparse,
  :sphere_version => :2dsphereIndexVersion',
  :storage_engine => :storageEngine,
  :text_version => :textIndexVersion,
  :unique => :unique,
  :version => :v,
  :weights => :weights,
  :collation => :collation
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create the new index view.

Examples:

Create the new index view.

View::Index.new(collection)

Parameters:

  • collection (Collection)

    The collection.

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

    Options for getting a list of indexes. Only relevant for when the listIndexes command is used with server versions >=2.8.

Options Hash (options):

  • :batch_size (Integer)

    The batch size for results returned from the listIndexes command.

Since:

  • 2.0.0



213
214
215
216
# File 'lib/mongo/index/view.rb', line 213

def initialize(collection, options = {})
  @collection = collection
  @batch_size = options[:batch_size]
end

Instance Attribute Details

#batch_sizeInteger (readonly)

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

Returns:

  • (Integer)

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

Since:

  • 2.0.0



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

def batch_size
  @batch_size
end

#collectionCollection (readonly)

Returns collection The indexes collection.

Returns:

  • (Collection)

    collection The indexes collection.

Since:

  • 2.0.0



26
27
28
# File 'lib/mongo/index/view.rb', line 26

def collection
  @collection
end

Instance Method Details

#create_many(*models) ⇒ Result

Note:

On MongoDB 3.0.0 and higher, the indexes will be created in parallel on the server.

Creates multiple indexes on the collection.

Examples:

Create multiple indexes.

view.create_many([
  { key: { name: 1 }, unique: true },
  { key: { age: -1 }, background: true }
])

Parameters:

  • models (Array<Hash>)

    The index specifications. Each model MUST include a :key option.

Returns:

  • (Result)

    The result of the command.

Since:

  • 2.0.0



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

def create_many(*models)
  server = next_primary
  spec = {
          indexes: normalize_models(models.flatten, server),
          db_name: database.name,
          coll_name: collection.name
         }

  spec[:write_concern] = write_concern if server.features.collation_enabled?
  Operation::Write::CreateIndex.new(spec).execute(server)
end

#create_one(keys, options = {}) ⇒ Result

Note:

Note that the options listed may be subset of those available.

Creates an index on the collection.

See the MongoDB documentation for a full list of supported options by server version.

Examples:

Create a unique index on the collection.

view.create_one({ name: 1 }, { unique: true })

Parameters:

  • keys (Hash)

    A hash of field name/direction pairs.

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

    Options for this index.

Options Hash (options):

  • :unique (true, false) — default: false

    If true, this index will enforce a uniqueness constraint on that field.

  • :background (true, false) — default: false

    If true, the index will be built in the background (only available for server versions >= 1.3.2 )

  • :drop_dups (true, false) — default: false

    If creating a unique index on this collection, this option will keep the first document the database indexes and drop all subsequent documents with duplicate values on this field.

  • :bucket_size (Integer) — default: nil

    For use with geoHaystack indexes. Number of documents to group together within a certain proximity to a given longitude and latitude.

  • :max (Integer) — default: nil

    Specify the max latitude and longitude for a geo index.

  • :min (Integer) — default: nil

    Specify the min latitude and longitude for a geo index.

  • :partial_filter_expression (Hash)

    Specify a filter for a partial index.

Returns:

  • (Result)

    The response.

Since:

  • 2.0.0



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

def create_one(keys, options = {})
  create_many({ key: keys }.merge(options))
end

#drop_allResult

Drop all indexes on the collection.

Examples:

Drop all indexes on the collection.

view.drop_all

Returns:

  • (Result)

    The response.

Since:

  • 2.0.0



94
95
96
# File 'lib/mongo/index/view.rb', line 94

def drop_all
  drop_by_name(Index::ALL)
end

#drop_one(name) ⇒ Result

Drop an index by its name.

Examples:

Drop an index by its name.

view.drop_one('name_1')

Parameters:

  • name (String)

    The name of the index.

Returns:

  • (Result)

    The response.

Raises:

Since:

  • 2.0.0



81
82
83
84
# File 'lib/mongo/index/view.rb', line 81

def drop_one(name)
  raise Error::MultiIndexDrop.new if name == Index::ALL
  drop_by_name(name)
end

#each(&block) ⇒ Object

Iterate over all indexes for the collection.

Examples:

Get all the indexes.

view.each do |index|
  ...
end

Since:

  • 2.0.0



190
191
192
193
194
195
196
197
# File 'lib/mongo/index/view.rb', line 190

def each(&block)
  server = next_primary(false)
  cursor = Cursor.new(self, send_initial_query(server), server).to_enum
  cursor.each do |doc|
    yield doc
  end if block_given?
  cursor
end

#get(keys_or_name) ⇒ Hash

Convenience method for getting index information by a specific name or spec.

Examples:

Get index information by name.

view.get('name_1')

Get index information by the keys.

view.get(name: 1)

Parameters:

  • keys_or_name (Hash, String)

    The index name or spec.

Returns:

  • (Hash)

    The index information.

Since:

  • 2.0.0



176
177
178
179
180
# File 'lib/mongo/index/view.rb', line 176

def get(keys_or_name)
  find do |index|
    (index[NAME] == keys_or_name) || (index[KEY] == normalize_keys(keys_or_name))
  end
end