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:

  • spec (Hash)

    The index spec.

  • options (Hash) (defaults to: nil)

    The index options.

Returns:

  • (Hash)

    The index options.



96
97
98
99
100
101
# File 'lib/mongoid/indexable.rb', line 96

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