Indexes support the green resolution of queries. Without indexes, MongoDB should experiment each report of a collection to select those documents that match the query declaration. This scan is exceedingly inefficient and require MongoDB to process a huge extent of information.
Indexes are unique records systems, that shop a small part of the records set in an smooth-to-traverse form. The index stores the fee of a particular discipline or set of fields, ordered by the cost of the field as precise in the index.
The createIndex() Method
To create an index, you need to apply createIndex() method of MongoDB.
Syntax
The basic syntax of createIndex() approach is as follows().
>db.COLLECTION_NAME.createIndex({KEY:1})
Here secret is the name of the sphere on which you want to create index and 1 is for ascending order. To create index in descending order you want to use -1.
Example
>db.mycol.createIndex({"title":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>
In createIndex() approach you could bypass more than one fields, to create index on multiple fields.
>db.mycol.createIndex({"title":1,"description":-1})
>
This technique additionally accepts listing of options (which can be non-obligatory). Following is the listing −
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 | Creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing 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. |
sparse | Boolean | If true, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). The default value is false. |
expireAfterSeconds | integer | Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection. |
weights | document | The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score. |
default_language | string | For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. The default value is English. |
language_override | string | For a text index, specify the name of the field in the document that contains, the language to override the default language. The default value is language. |
The dropIndex() approach
You can drop a selected index the use of the dropIndex() technique of MongoDB.
Syntax
The simple syntax of DropIndex() technique is as follows().
>db.COLLECTION_NAME.dropIndex({KEY:1})
Here key is the name of the document on which you need to create index and 1 is for ascending order. To create index in descending order you need to apply -1.
Example
> db.mycol.dropIndex({"title":1})
{
"ok" : 0,
"errmsg" : "can't find index with key: { title: 1.0 }",
"code" : 27,
"codeName" : "IndexNotFound"
}
The dropIndexes() technique
This method deletes more than one (designated) indexes on a group.
Syntax
The basic syntax of DropIndexes() method is as follows() −
>db.COLLECTION_NAME.dropIndexes()
Example
Assume we have created 2 indexes in the named mycol collection as proven underneath −
> db.mycol.createIndex({"title":1,"description":-1})
Following instance gets rid of the above created indexes of mycol −
>db.mycol.dropIndexes({"title":1,"description":-1})
{ "nIndexesWas" : 2, "ok" : 1 }
>
The getIndexes() method
This approach returns the description of all of the indexes int the collection.
Syntax
Following is the fundamental syntax od the getIndexes() approach −
db.COLLECTION_NAME.getIndexes()
Example
Assume we've created 2 indexes inside the named mycol collection as proven beneath −
> db.mycol.createIndex({"title":1,"description":-1})
Following instance retrieves all of the indexes in the series mycol −
> db.mycol.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.mycol"
},
{
"v" : 2,
"key" : {
"title" : 1,
"description" : -1
},
"name" : "title_1_description_-1",
"ns" : "test.mycol"
}
]
>