Navigation

IndexesΒΆ

You can define indexes on documents using the index macro. Provide the key for the index along with a direction. For additional options, supply them in a second options hash parameter.

class Person
  include Mongoid::Document
  field :ssn

  index({ ssn: 1 }, { unique: true, name: "ssn_index" })
end

You can define indexes on embedded document fields as well.

class Person
  include Mongoid::Document
  embeds_many :addresses
  index "addresses.street" => 1
end

You can index on multiple fields and provide direction.

class Person
  include Mongoid::Document
  field :first_name
  field :last_name

  index({ first_name: 1, last_name: 1 }, { unique: true })
end

Indexes can be sparse:

class Person
  include Mongoid::Document
  field :ssn

  index({ ssn: -1 }, { sparse: true })
end

Indexes can be run in the background in cases where they may take some time:

class Person
  include Mongoid::Document
  field :ssn
  index({ ssn: 1 }, { unique: true, background: true })
end

For unique indexes that are defined for a column that might already have duplicate values, you can drop the duplicate entries:

class Person
  include Mongoid::Document
  field :ssn
  index({ ssn: 1 }, { unique: true, drop_dups: true })
end

For geospatial indexes, make sure the field you are indexing is an Array.

class Person
  include Mongoid::Document
  field :location, type: Array

  index({ location: "2d" }, { min: -200, max: 200 })
end

Indexes can be scoped to a specific database.

class Person
  include Mongoid::Document
  field :ssn
  index({ ssn: 1 }, { database: "users", unique: true, background: true })
end

You can have Mongoid define indexes for you on “foreign key” fields for relational associations. This only works on the relation macro that the foreign key is stored on.

class Comment
  include Mongoid::Document
  belongs_to :post, index: true
  has_and_belongs_to_many :preferences, index: true
end

When you want to create the indexes in the database, use the provided rake task.

$ rake db:mongoid:create_indexes

Mongoid also provides a rake task to delete all secondary indexes.

$ rake db:mongoid:remove_indexes

These create/remove indexes commands also works for just one model by running

Model.create_indexes

or

Model.remove_indexes