Module: Mongoid::Indexes::ClassMethods

Defined in:
lib/mongoid/indexes.rb

Instance Method Summary (collapse)

Instance Method Details

- (true) add_indexes

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

Examples:

Add Mongoid internal indexes.

Person.add_indexes

Returns:

  • (true)

    If the operation succeeded.

Since:

  • 1.0.0



55
56
57
58
59
60
# File 'lib/mongoid/indexes.rb', line 55

def add_indexes
  if hereditary? && !index_options[{ _type: 1 }]
    index _type: 1, options: { unique: false, background: true }
  end
  true
end

- (true) create_indexes

Send the actual index creation comments to the MongoDB driver

Examples:

Create the indexes for the class.

Person.create_indexes

Returns:

  • (true)

    If the operation succeeded.

Since:

  • 1.0.0



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

def create_indexes
  return unless index_options
  index_options.each_pair do |spec, options|
    collection.indexes.create(spec, options)
  end and true
end

- (Hash) index(spec)

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

Examples:

Create a basic index.

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

Parameters:

  • name (Symbol)

    The name of the field.

  • options (Hash)

    The index options.

Returns:

  • (Hash)

    The index options.

Since:

  • 1.0.0



78
79
80
81
# File 'lib/mongoid/indexes.rb', line 78

def index(spec)
  Validators::Options.validate(self, spec)
  index_options[spec] = normalize_index_options(spec)
end

- (true) remove_indexes

Send the actual index removal comments to the MongoDB driver, but lets _id untouched.

Examples:

Remove the indexes for the class.

Person.remove_indexes

Returns:

  • (true)

    If the operation succeeded.

Since:

  • 3.0.0



39
40
41
42
43
44
# File 'lib/mongoid/indexes.rb', line 39

def remove_indexes
  collection.indexes.each do |spec|
    next if spec["name"] == "_id_"
    collection.indexes.drop(spec["key"])
  end and true
end