Easy Tutorial
❮ Mongodb Connections Nosql ❯

MongoDB Indexes

Indexes can greatly improve query efficiency. Without indexes, MongoDB must scan every document in a collection and select those that match the query criteria.

This full collection scan is very inefficient, especially when dealing with large amounts of data, where queries can take tens of seconds or even minutes, which is fatal for website performance.

Indexes are a special data structure that stores data in an easily traversable and readable format. Indexes are a structure that sorts the values of one or more columns in a database table.


createIndex() Method

MongoDB uses the createIndex() method to create indexes.

>

Note that before version 3.0.0, the method for creating indexes was db.collection.ensureIndex(), and later versions used the db.collection.createIndex() method. ensureIndex() is still usable but is now just an alias for createIndex().

Syntax

The basic syntax for the createIndex() method is as follows:

>db.collection.createIndex(keys, options)

In this syntax, the key value is the field you want to create an index on, with 1 specifying an ascending index. To create a descending index, specify -1.

Example

>db.col.createIndex({"title":1})
>

You can also set up multiple fields to create an index (referred to as a compound index in relational databases).

>db.col.createIndex({"title":1,"description":-1})
>

createIndex() accepts optional parameters, which are listed below:

Parameter Type Description
background Boolean Index creation blocks other database operations. background can specify creating the index in the background, adding the "background" optional parameter. "background" defaults to false.
unique Boolean Whether the index is unique. Specify true to create a unique index. Defaults to false.
name string The name of the index. If not specified, MongoDB generates an index name by concatenating the index fields and their sort order.
dropDups Boolean Deprecated in 3.0+ versions. Whether to remove duplicate records when creating a unique index. Specify true to create a unique index. Defaults to false.
sparse Boolean Whether to disable indexing for fields not present in the documents. This parameter is important; if set to true, documents without the indexed field will not be queried. Defaults to false.
expireAfterSeconds integer Specifies a value in seconds to set the TTL (Time To Live) and set the collection's lifespan.
v index version The version number of the index. The default index version depends on the version of mongod running when the index was created.
weights document The index weight value, ranging from 1 to 99,999, indicating the relative score weight of this index compared to other index fields.
default_language string For text indexes, this parameter determines the list of stop words and the rules for stemming and tokenization. Defaults to English.
language_override string For text indexes, this parameter specifies the field name in the document that overrides the default language. Defaults to language.

Example

Creating an index in the background:

db.values.createIndex({open: 1, close: 1}, {background: true})

By adding the background:true option when creating an index, the creation process runs in the background.

❮ Mongodb Connections Nosql ❯