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(key) ⇒ 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.
70 71 72 73 74 75 |
# File 'lib/mongoid/indexable.rb', line 70 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
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mongoid/indexable.rb', line 28 def create_indexes return unless index_specifications index_specifications.each do |spec| key, = spec.key, spec. if database = [:database] with(consistency: :strong, database: database). collection.indexes.create(key, .except(:database)) else with(consistency: :strong).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.
92 93 94 95 96 97 |
# File 'lib/mongoid/indexable.rb', line 92 def index(spec, = nil) specification = Specification.new(self, spec, ) if !index_specifications.include?(specification) index_specifications.push(specification) end end |
#index_specification(key) ⇒ Specification
Get an index specification for the provided key.
109 110 111 |
# File 'lib/mongoid/indexable.rb', line 109 def index_specification(key) index_specifications.detect{ |spec| spec.fields == key.keys } end |
#remove_indexes ⇒ true
Send the actual index removal comments to the MongoDB driver, but lets _id untouched.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mongoid/indexable.rb', line 50 def remove_indexes indexed_database_names.each do |database| collection = with(consistency: :strong, database: database).collection collection.indexes.each do |spec| unless spec["name"] == "_id_" collection.indexes.drop(spec["key"]) end end end and true end |