MongoDB Indexing PDF
MongoDB Indexing PDF
MongoDB - Indexing
Advertisements
Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan
every document of a collection to select those documents that match the query statement.
This scan is highly inefficient and require MongoDB to process a large volume of data.
Indexes are special data structures, that store a small portion of the data set in an easy-
to-traverse form. The index stores the value of a specific field or set of fields, ordered by
the value of the field as specified in the index.
Syntax
The basic syntax of ensureIndex() method is as follows().
>db.COLLECTION_NAME.ensureIndex({KEY:1})
Here key is the name of the field on which you want to create index and 1 is for ascending
order. To create index in descending order you need to use -1.
Example
>db.mycol.ensureIndex({"title":1})
>
In ensureIndex() method you can pass multiple fields, to create index on multiple fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
>
ensureIndex() method also accepts list of options (which are optional). Following is the
list −
background Boolean Builds the index in the background so that building an index
https://www.tutorialspoint.com/mongodb/mongodb_indexing.htm 1/3
2/28/2018 MongoDB Indexing
index The index version number. The default index version depends
v
version on the version of MongoDB running when creating the index.
For a text index, the language that determines the list of stop
default_language string words and the rules for the stemmer and tokenizer. The
default value is english.
Advertisements
YouTube 52K
https://www.tutorialspoint.com/mongodb/mongodb_indexing.htm 2/3
2/28/2018 MongoDB Indexing
https://www.tutorialspoint.com/mongodb/mongodb_indexing.htm 3/3