123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Indexable::ClassMethods

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Defined in: lib/mongoid/indexable.rb

Instance Method Summary

Instance Method Details

#add_indexestrue

Add the default indexes to the root document if they do not already exist. Currently this is only _type.

Examples:

Add Mongoid internal indexes.

Person.add_indexes

Returns:

  • (true)

    If the operation succeeded.

[ GitHub ]

  
# File 'lib/mongoid/indexable.rb', line 76

def add_indexes
  if hereditary? && !index_keys.include?(self.discriminator_key.to_sym => 1)
    index({ self.discriminator_key.to_sym => 1 }, unique: false, background: true)
  end
  true
end

#create_indexestrue

Send the actual index creation comments to the MongoDB driver

Examples:

Create the indexes for the class.

Person.create_indexes

Returns:

  • (true)

    If the operation succeeded.

[ GitHub ]

  
# File 'lib/mongoid/indexable.rb', line 27

def create_indexes
  return unless index_specifications

  default_options = {background: Config.background_indexing}

  index_specifications.each do |spec|
    key, options = spec.key, default_options.merge(spec.options)
    if database = options[:database]
      with(database: database) do |klass|
        klass.collection.indexes(session: _session).create_one(key, options.except(:database))
      end
    else
      collection.indexes(session: _session).create_one(key, options)
    end
  end and true
end

#index(spec, options = nil) ⇒ Hash

Adds an index definition for the provided single or compound keys.

Examples:

Create a basic index.

class Person
  include Mongoid::Document
  field :name, type: String
  index({ name: 1 }, { background: true })
end

Parameters:

  • spec (Hash)

    The index spec.

  • options (Hash) (defaults to: nil)

    The index options.

Returns:

  • (Hash)

    The index options.

[ GitHub ]

  
# File 'lib/mongoid/indexable.rb', line 96

def index(spec, options = nil)
  specification = Specification.new(self, spec, options)
  if !index_specifications.include?(specification)
    index_specifications.push(specification)
  end
end

#index_keysArray<Hash> (private)

This method is for internal use only.

Gets a list of index specification keys.

Examples:

Get the specification key list.

Model.index_keys

Returns:

[ GitHub ]

  
# File 'lib/mongoid/indexable.rb', line 144

def index_keys
  index_specifications.map(&:key)
end

#index_specification(index_hash, index_name = nil) ⇒ Specification

Get an index specification for the provided key.

Examples:

Get the index specification.

Model.index_specification(name: 1)

Parameters:

  • index_hash (Hash)

    The index key/direction pair.

  • index_name (String) (defaults to: nil)

    The index name.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/indexable.rb', line 112

def index_specification(index_hash, index_name = nil)
  index = OpenStruct.new(fields: index_hash.keys, key: index_hash)
  index_specifications.detect do |spec|
    spec == index || (index_name && index_name == spec.name)
  end
end

#indexed_database_namesArray<String> (private)

This method is for internal use only.

Get the names of all databases for this model that have index definitions.

Examples:

Get the indexed database names.

Model.indexed_database_names

Returns:

[ GitHub ]

  
# File 'lib/mongoid/indexable.rb', line 130

def indexed_database_names
  index_specifications.map do |spec|
    spec.options[:database] || database_name
  end.uniq
end

#remove_indexestrue

Send the actual index removal comments to the MongoDB driver, but lets _id untouched.

Examples:

Remove the indexes for the class.

Person.remove_indexes

Returns:

  • (true)

    If the operation succeeded.

[ GitHub ]

  
# File 'lib/mongoid/indexable.rb', line 51

def remove_indexes
  indexed_database_names.each do |database|
    with(database: database) do |klass|
      begin
        klass.collection.indexes(session: _session).each do |spec|
          unless spec["name"] == "_id_"
            klass.collection.indexes(session: _session).drop_one(spec["key"])
            logger.info(
              "MONGOID: Removed index '#{spec["name"]}' on collection " +
              "'#{klass.collection.name}' in database '#{database}'."
            )
          end
        end
      rescue Mongo::Error::OperationFailure; end
    end
  end and true
end