Method: Mongoid::Indexable::ClassMethods#index

Defined in:
lib/mongoid/indexable.rb

#index(spec, options = nil) ⇒ Hash

Adds an index definition for the provided single or compound keys.

Examples:

Create a basic index.

class Person
  include Mongoid::Document
  field :name, type: String
  index({ name: 1 }, { background: true })
end

Parameters:

  • name (Symbol)

    The name of the field.

  • options (Hash) (defaults to: nil)

    The index options.

Returns:

  • (Hash)

    The index options.

Since:

  • 1.0.0



99
100
101
102
103
104
# File 'lib/mongoid/indexable.rb', line 99

def index(spec, options = nil)
  specification = Specification.new(self, spec, options)
  if !index_specifications.include?(specification)
    index_specifications.push(specification)
  end
end