Module: Mongoid::Indexable::ClassMethods
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
| Extended In: | |
| Defined in: | lib/mongoid/indexable.rb | 
Instance Method Summary
- 
    
      #add_indexes  ⇒ true 
    
    Add the default indexes to the root document if they do not already exist. 
- 
    
      #create_indexes  ⇒ true 
    
    Send the actual index creation comments to the MongoDB driver. 
- 
    
      #index(spec, options = nil)  ⇒ Hash 
    
    Adds an index definition for the provided single or compound keys. 
- 
    
      #index_specification(index_hash, index_name = nil)  ⇒ Specification 
    
    Get an index specification for the provided key. 
- 
    
      #remove_indexes  ⇒ true 
    
    Send the actual index removal comments to the MongoDB driver, but lets _id untouched. 
- 
    
      #index_keys  ⇒ Array<Hash> 
    
    private
    Internal use only
    Internal use only
    Gets a list of index specification keys. 
- 
    
      #indexed_database_names  ⇒ Array<String> 
    
    private
    Internal use only
    Internal use only
    Get the names of all databases for this model that have index definitions. 
Instance Method Details
    #add_indexes  ⇒ true 
  
Add the default indexes to the root document if they do not already exist. Currently this is only _type.
# 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_indexes  ⇒ true 
  
Send the actual index creation comments to the MongoDB driver
# File 'lib/mongoid/indexable.rb', line 27
def create_indexes return unless index_specifications = {background: Config.background_indexing} index_specifications.each do |spec| key, = spec.key, .merge(spec.) if database = [:database] with(database: database) do |klass| klass.collection.indexes(session: _session).create_one(key, .except(:database)) end else collection.indexes(session: _session).create_one(key, ) end end and true end
#index(spec, options = nil) ⇒ Hash
Adds an index definition for the provided single or compound keys.
# File 'lib/mongoid/indexable.rb', line 96
def index(spec, = nil) specification = Specification.new(self, spec, ) # the equality test for Indexable::Specification instances does not # consider any options, which means names are not compared. This means # that an index with different options from another, and a different # name, will be silently ignored unless duplicate index declarations # are allowed. if Mongoid.allow_duplicate_index_declarations || !index_specifications.include?(specification) index_specifications.push(specification) end end
#index_keys ⇒ Array<Hash> (private)
Gets a list of index specification keys.
# File 'lib/mongoid/indexable.rb', line 149
def index_keys index_specifications.map(&:key) end
#index_specification(index_hash, index_name = nil) ⇒ Specification
Get an index specification for the provided key.
# File 'lib/mongoid/indexable.rb', line 118
def index_specification(index_hash, index_name = nil) index_specifications.detect do |spec| spec.superficial_match?(key: index_hash, name: index_name) end end
#indexed_database_names ⇒ Array<String> (private)
Get the names of all databases for this model that have index definitions.
# File 'lib/mongoid/indexable.rb', line 135
def indexed_database_names index_specifications.map do |spec| spec.[:database] || database_name end.uniq end
    #remove_indexes  ⇒ true 
  
Send the actual index removal comments to the MongoDB driver, but lets _id untouched.
# 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