MongoDB – Indexing

Indexes help the resolution of queries effectively. MongoDB must scan every document in a set without indexes, in necessary to pick those documents that fit the query statement. This scan is extremely inefficient and allows a wide data volume to be processed by MongoDB.

Indexes help the resolution of queries effectively. MongoDB must scan every document in a set without indexes, in necessary to pick those documents that fit the query statement. This scan is extremely inefficient and allows a wide data volume to be processed by MongoDB.

Specific data structures that store a small portion of the data set in an easy-to-traverse type are indexes. The index stores the value of a particular field or fieldset, ordered by the field value as defined in the index.

The createIndex() Method

You need to use the createIndex() MongoDB method to build an index.

Syntax

The basic syntax for a method called createIndex() is as follows.

>db.COLLECTION_NAME.createIndex({KEY:1})

The main here is the name of the area that you want to create an index for, and the ascending order is 1. You need to use -1 to create an index in descending order.

Example

>db.mycol.createIndex({"title":1})
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}
>

You can pass multiple fields using the createIndex() method to build an index on multiple fields.

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

This method also accepts a list of options (which are optional). Following is the list −

ParameterTypeDescription
backgroundBooleanCreate the index in the background so that it does not interfere with other database activities. True is used to build in the background. The default has the meaning false.
uniqueBooleanCreates a unique index such that adding documents where the index key or keys match an existing value in the index would not be recognized by the collection. Specify true to create an index that is unique. The meaning of the default is false.
namestringThe name of this index. If unspecified, by concatenating the names of the indexed fields and the sort order, MongoDB will create an index name.
sparseBooleanOnly documents with the specified field are listed in the index if they are valid. These indexes take up less space, but they behave differently in certain situations (particularly sorts). The default’s meaning is incorrect.
expireAfterSecondsintegerSpecifies a value to control how long MongoDB holds documents in this collection, in seconds, as a TTL..
weightsdocumentThe weight is a number between 1 and 99,999 and denotes the value of the field in terms of the score compared to the other indexed fields.
default_languagestringA text index is created using the language that specifies the list of stop words as well as the rules for the stemmer and tokenizer. English is the default setting.
language_overridestringTo override the default language for a text index, enter a field name in the document that includes the language. Language is the default setting.

The dropIndex() method

A specific index can be drop using the MongoDB dropIndex() method.

Syntax

The basic syntax of DropIndex() method is as follows().

>db.COLLECTION_NAME.dropIndex({KEY:1})

The main here is the name of the file for which an index is to be created, and the ascending order is 1. To create an index in descending order, use -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() method

This method deletes multiple (specified) indexes on a collection.

Syntax

The basic syntax of DropIndexes() method is as follows() −

>db.COLLECTION_NAME.dropIndexes()

Example

Suppose we have created 2 indexes in the collection called mycol, as shown below.

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

Following example removes the above created indexes of mycol −

>db.mycol.dropIndexes({"title":1,"description":-1})
{ "nIndexesWas" : 2, "ok" : 1 }
>

The getIndexes() method

This method returns the definition of all of the collection’s indexes.

Syntax

This is the basic syntax of the method getIndexes() –

db.COLLECTION_NAME.getIndexes()

Example

Suppose we have created 2 indexes in the collection called mycol, as shown below.

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

Following example retrieves all the indexes in the collection 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"
	}
]
>

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *