Module: Mongoid::Indexable::ClassMethods
- Defined in:
- lib/mongoid/indexable.rb
Overview
Instance Method Summary collapse
-
#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.
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.
84 85 86 87 88 89 |
# File 'lib/mongoid/indexable.rb', line 84 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/mongoid/indexable.rb', line 31 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.
106 107 108 109 110 111 |
# File 'lib/mongoid/indexable.rb', line 106 def index(spec, = nil) specification = Specification.new(self, spec, ) if !index_specifications.include?(specification) index_specifications.push(specification) end end |
#index_specification(index_hash, index_name = nil) ⇒ Specification
Get an index specification for the provided key.
124 125 126 127 128 129 |
# File 'lib/mongoid/indexable.rb', line 124 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 |
#remove_indexes ⇒ true
Send the actual index removal comments to the MongoDB driver, but lets _id untouched.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongoid/indexable.rb', line 57 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 |