Module: Mongoid::Indexes::ClassMethods

Defined in:
lib/mongoid/indexes.rb

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



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 }, { 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



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

#index(spec, options = nil) ⇒ Hash

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



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

def index(spec, options = nil)
  Validators::Options.validate(self, spec, options || {})
  index_options[spec] = normalize_index_options(options)
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



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