Module: Mongoid::Indexes::ClassMethods

Defined in:
lib/mongoid/indexes.rb

Overview

:nodoc

Instance Method Summary collapse

Instance Method Details

#add_indexesObject

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



23
24
25
26
27
28
# File 'lib/mongoid/indexes.rb', line 23

def add_indexes
  if hereditary? && !index_options[:_type]
    self.index_options[:_type] = {:unique => false, :background => true}
  end
  create_indexes if Mongoid.autocreate_indexes
end

#create_indexesObject

Send the actual index creation comments to the MongoDB driver



13
14
15
16
17
18
19
# File 'lib/mongoid/indexes.rb', line 13

def create_indexes
  return unless index_options
  current_collection = self._collection || set_collection
  index_options.each do |name, options|
    current_collection.create_index(name, options)
  end
end

#index(name, options = { :unique => false }) ⇒ Object

Adds an index on the field specified. Options can be :unique => true or :unique => false. It will default to the latter.



32
33
34
35
# File 'lib/mongoid/indexes.rb', line 32

def index(name, options = { :unique => false })
  self.index_options[name] = options
  create_indexes if Mongoid.autocreate_indexes
end