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) ⇒ 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.
71 72 73 74 75 76 |
# File 'lib/mongoid/indexable.rb', line 71 def add_indexes if hereditary? && !index_keys.include?(_type: 1) index({ _type: 1 }, unique: false, background: true) end true end |
#create_indexes ⇒ true
Send the actual index creation comments to the MongoDB driver
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mongoid/indexable.rb', line 29 def create_indexes return unless index_specifications index_specifications.each do |spec| key, = spec.key, spec. if database = [:database] with(read: :primary, database: database). collection.indexes.create(key, .except(:database)) else with(read: :primary).collection.indexes.create(key, ) end end and true end |
#index(spec, options = nil) ⇒ Hash
Adds an index definition for the provided single or compound keys.
93 94 95 96 97 98 |
# File 'lib/mongoid/indexable.rb', line 93 def index(spec, = nil) specification = Specification.new(self, spec, ) if !index_specifications.include?(specification) index_specifications.push(specification) end end |
#index_specification(index_hash) ⇒ Specification
Get an index specification for the provided key.
110 111 112 113 |
# File 'lib/mongoid/indexable.rb', line 110 def index_specification(index_hash) index = OpenStruct.new(fields: index_hash.keys, key: index_hash) index_specifications.detect { |spec| spec == index } end |
#remove_indexes ⇒ true
Send the actual index removal comments to the MongoDB driver, but lets _id untouched.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongoid/indexable.rb', line 51 def remove_indexes indexed_database_names.each do |database| collection = with(read: :primary, database: database).collection collection.indexes.each do |spec| unless spec["name"] == "_id_" collection.indexes.drop(spec["key"]) end end end and true end |