Chat Zalo Chat Messenger Phone Number Đăng nhập
MongoDB Tutorial

MongoDB Tutorial

Indexes support efficient query resolution. Without indexes, MongoDB must examine each document in a collection to select those documents that match the query statement. This analysis is highly inefficient and requires MongoDB to process a large volume of data.

Indexes are special data structures that store a small portion of the dataset in an easy-to-traverse form. The index stores the value of a specific field or set of fields, sorted by the value of the field specified in the index.

The createIndex() method

To create an index, you must use the

MongoDB createIndex() method.


The basic syntax of the createIndex() method is as follows().


COLLECTION_NAME.createIndex({KEY:1}) Here the key is the

name of the field in which you want to create the index and 1 is for the ascending order. To create an index in descending order, you must use -1.


>db.mycol.createIndex({“title”:1}) { “createdCollectionAutomatically” : false, “numIndexesBefore” : 1, “numIndexesAfter” : 2, “ok” : 1 } > In the createIndex() method, you can pass multiple fields,

to create an index on multiple fields. >db.mycol.createIndex

({“title”:1,”description”:-1}) >

This method also accepts the list of options (which are optional). The following is the list:

Description of the parameter typeboolebackground medium Generates the index in the background so that creating an index does not block other database activities. Specify true to compile in the background. The default value is false. Single Boolean Creates a unique index so that the collection does not accept the 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 string Index Name. If not specified, MongoDB generates an index name by concatenating the indexed field names and sort order. Sparse Boolean If true, the index refers only to documents with the specified field. These indices use less space, but behave differently in some situations (particularly types). The default value is false. expireAfterSeconds integer Specifies a value, in seconds, as TTL to control how long MongoDB retains documents in this collection.weights document The weight is a number between 1 and 99,999 and denotes the importance of the field in relation to the other indexed fields in terms of the string score.default_language For a full-text index, The language that determines the list of empty words and the rules for the stemmer and tokenizer. The default value is English.language_override string For a full-text index, specify the name of the field in the document it contains, the language to replace the default language. The default value is language. The dropIndex() method

You can delete a particular index using the MongoDB dropIndex() method



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



Here, “key” is the name of the file on which you want to delete an existing index. Instead of the index specification document (syntax above), you can also specify the index name directly as

: dropIndex(“name_of_the_index”)


> 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 from a collection.


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

db. COLLECTION_NAME.dropIndexes()


Suppose we have created 2 indexes in the named mycol collection as shown below

− > db.mycol.createIndex({“title”:1,”description”:-1})

The following example deletes previously created indexes

from mycol − >db.mycol.dropIndexes({“title”:1,”description”:-1}) { “nIndexesWas” : 2, “ok” : 1 } >

The getIndexes() method

This method returns the description of all indexes in the collection.


The following is the basic syntax of the getIndexes() − db method

. COLLECTION_NAME.getIndexes()


Suppose we have created

2 indexes in the mycol collection named as shown below − > db.mycol.createIndex({“title”:1,”description”:-1})

The following example retrieves all 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” } ] >

Contact US