As we know, the index is a technique to arrange data in a database that supports the efficient resolution of queries. If we run a query to select data, MongoDB does scan every document of the collection. Scanning of all documents may affect the performance of MongoDB when it processes the large volume of data.
According to the MongoDB document, “Indexes are special data structures that store a small portion of the collection’s 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. The ordering of the index entries supports efficient equality matches and range-based query operations. Also, MongoDB can return sorted results by using the ordering in the index.”
Create an Index
To create an index use db.collection_name.createIndex().
Syntax
db.collection_name.createIndex( <key and index type specification>, <options> )
|
Note: MongoDB creates an index, using db.collection_name.create() method when an index’s same specification does not exist.
Example
Suppose we have a collection EMP as below where we wish to create an index on the Name field.
/* 1 */
{
"_id" : "1",
"Name" : "Dilip",
"Salary" : 50000.0,
"Project" : "Codeafri Mongodb",
"DOJ" : ISODate("2014-12-01T20:19:55.782Z")
}
/* 2 */
{
"_id" : "2",
"Name" : "Vipul",
"Salary" : 30000.0,
"Project" : "Codeafri UI",
"DOJ" : ISODate("2015-11-01T20:19:55.782Z")
}
/* 3 */
{
"_id" : "3",
"Name" : "Ashish",
"Salary" : 100000.0,
"Project" : "Codeafri Project architect",
"DOJ" : ISODate("2013-10-01T20:19:55.782Z")
}
|
Query
Now I want to create an index on the Name field. Please see the following query to create an index.
db.records.createIndex( { "Name": 1 } )
|
Output
/* 1 */
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1.0
}
|
createIndex() method accepts a list of options (which is optional) see the following list.
Parameter |
Type |
Description |
background
|
boolean
|
Builds the index in the background so that building an index does not block other database activities. Specify true to build in the background. The default value is false.
|
unique
|
boolean
|
It creates a unique index so that the collection will not accept the insertion of documents where the index key or keys match a current value in the index. Specify true to create a unique index. The default value is false.
|
name
|
string
|
The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.
Whether user-specified or MongoDB generated, index names including their full namespace (i.e., database.collection) cannot be longer than the Index Name Limit.
|
a partial filter expression
|
document
|
Optional. If specified, the index only references documents that match the filter expression.
|
Types of Index in MongoDB
1-Single Field Index
2-Compound Index
3-Multikey Index
4-Geospatial Index
5-Text Index
6-Hashed index
Index Property
1-Unique Indexes
2-Partial Indexes
3-Sparse Indexes
4-TTL Indexes