Home >>MongoDB Tutorial >MongoDB Indexing

MongoDB Indexing

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.

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 field set, 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 list of options (which are optional). Following is the list −

Parameter Type Description
background Boolean Create the index in the background so that other database activities are not prevented by building an index. To build in the background, specify true. The meaning of the default is false.
unique Boolean Creates 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.
name string The name of this index. If unspecified, by concatenating the names of the indexed fields and the sort order, MongoDB will create an index name.
sparse Boolean If valid, only documents with the field listed are listed in the index. These indexes use less space, but in some situations (particularly sorts) they behave differently. The meaning of the default is wrong.
expireAfterSeconds integer Specifies a value to control how long MongoDB holds documents in this collection, in seconds, as a TTL..
weights document The 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_language string The language that specifies the list of stop words and the rules for the stemmer and tokenizer are used for a text index. English is the default value.
language_override string Enter a field name in the document that includes the language to override the default language for a text index. Language is the default value.

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 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.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"
	}
]
>


No Sidebar ads