<commit_before>.. _indexes:

****************
Index Management
****************

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 2
   :class: singlecol

Specifying Indexes
==================

You can define indexes on documents using the index macro. Provide the key for
the index along with a direction. Additional options can be supplied in the
second options hash parameter:

.. code-block:: ruby

    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:

.. code-block:: ruby

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

You can index on multiple fields and provide direction:

.. code-block:: ruby

    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:

.. code-block:: ruby

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

Indexes can be specified to be created in the background in cases where
the collection is large and index creation may take a long time:

.. code-block:: ruby

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

If the ``background_indexing`` :ref:`configuration option
<configuration-options>` is true, the indexes are created in the background
unless the ``background: false`` option is provided.

*Deprecated:* When using MongoDB 2.6, you can drop the duplicate entries
for unique indexes that are defined for a column that might
already have duplicate values by specifying the ``drop_dups`` option:

.. code-block:: ruby

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

The ``drop_dups`` option has been `removed as of MongoDB 3.0
<https://docs.mongodb.com/manual/release-notes/3.0-compatibility/#remove-dropdups-option>`_.

For geospatial indexes, make sure the field being indexed is of type Array:

.. code-block:: ruby

    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:

.. code-block:: ruby

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

Mongoid can define indexes on "foreign key" fields for associations.
This only works on the association macro that the foreign key is stored on:

.. code-block:: ruby

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

Index Management Rake Tasks
===========================

When you want to create the indexes in the database, use the provided
``db:mongoid::create_indexes`` Rake task:

.. code-block:: bash

    $ rake db:mongoid:create_indexes

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

.. code-block:: bash

    $ rake db:mongoid:remove_indexes

Note: the output of these Rake tasks goes to the default logger configured
by Rails. This is usually a file like ``log/development.log`` and not standard
output.

These create/remove indexes commands also works for just one model by running<commit_msg>Fix typo on indexes tutorial page (#4998)<commit_after>.. _indexes:

****************
Index Management
****************

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 2
   :class: singlecol

Specifying Indexes
==================

You can define indexes on documents using the index macro. Provide the key for
the index along with a direction. Additional options can be supplied in the
second options hash parameter:

.. code-block:: ruby

    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:

.. code-block:: ruby

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

You can index on multiple fields and provide direction:

.. code-block:: ruby

    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:

.. code-block:: ruby

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

Indexes can be specified to be created in the background in cases where
the collection is large and index creation may take a long time:

.. code-block:: ruby

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

If the ``background_indexing`` :ref:`configuration option
<configuration-options>` is true, the indexes are created in the background
unless the ``background: false`` option is provided.

*Deprecated:* When using MongoDB 2.6, you can drop the duplicate entries
for unique indexes that are defined for a column that might
already have duplicate values by specifying the ``drop_dups`` option:

.. code-block:: ruby

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

The ``drop_dups`` option has been `removed as of MongoDB 3.0
<https://docs.mongodb.com/manual/release-notes/3.0-compatibility/#remove-dropdups-option>`_.

For geospatial indexes, make sure the field being indexed is of type Array:

.. code-block:: ruby

    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:

.. code-block:: ruby

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

Mongoid can define indexes on "foreign key" fields for associations.
This only works on the association macro that the foreign key is stored on:

.. code-block:: ruby

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

Index Management Rake Tasks
===========================

When you want to create the indexes in the database, use the provided
``db:mongoid:create_indexes`` Rake task:

.. code-block:: bash

    $ rake db:mongoid:create_indexes

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

.. code-block:: bash

    $ rake db:mongoid:remove_indexes

Note: the output of these Rake tasks goes to the default logger configured
by Rails. This is usually a file like ``log/development.log`` and not standard
output.

These create/remove indexes commands also works for just one model by running
{'fraction_non_alphanumeric': 0.10307289513573102, 'fraction_numerical': 0.006614303431169905, 'mean_word_length': 3.2469280280866, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7456048', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1993, 'n_words': 859}