Module: Mongoid::Indexable::ClassMethods

Defined in:
lib/mongoid/indexable.rb

Overview

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#add_indexestrue

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


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_indexestrue

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


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, options = spec.key, spec.options
    if database = options[:database]
      with(consistency: :strong, database: database).
        collection.indexes.create(key, options.except(:database))
    else
      with(consistency: :strong).collection.indexes.create(key, options)
    end
  end and true
end

#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


92
93
94
95
96
97
# File 'lib/mongoid/indexable.rb', line 92

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

#index_specification(key) ⇒ Specification

Get an index specification for the provided key.

Examples:

Get the index specification.

Model.index_specification(name: 1)

Parameters:

  • key (Hash)

    The index key/direction pair.

Returns:

Since:

  • 4.0.0


109
110
111
# File 'lib/mongoid/indexable.rb', line 109

def index_specification(key)
  index_specifications.detect{ |spec| spec.fields == key.keys }
end

#remove_indexestrue

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


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