Index (Mong) in MongoDB

Index (Index) supports the resolution of queries more efficiently. Without an index, MongoDB must scan through all a Collection's Document to select the Documents that connect to the query command. This scan may be ineffective and requires MongoDB to handle a large amount of data.

Index (Index) supports the resolution of queries more efficiently. Without an index, MongoDB must scan through all a Collection's Document to select the Documents that connect to the query command. This scan may be ineffective and requires MongoDB to handle a large amount of data.

Indexes are special data structures that store a small part of the data set, making it easier to "dab" into Collection. The index stores the value of a specific field or set of fields, sorted by the value of the field as defined in the index.

The method ensureIndex () in MongoDB

To create an index, you need to use MongoDB's ensureIndex () method.

Syntax

The basic syntax of the ensureIndex () method is as follows:

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

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

For example

 > db . mycol . ensureIndex ({ "title" : 1 }) > 

In the ensureIndex () method , you can pass multiple fields, to create indexes on multiple fields, you use:

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

The method ensureIndex () also accepts the list of optional options, listed below:

ParametersTypeBoolean Description Build an index in the Background so that it does not interfere with other database operations. Specify true to build in Background. The default value is false unique Boolean Create a unique index so that the Collection will not accept the insertion of Documents whose key is connecting to an existing value in the index. Specify true to create unique index. The default value is false name String Name of the index. If not specified, MongoDB creates an index name by linking the names of the indexed fields and dropDups Boolean sequencing Create a dropDups index on a field that can have duplicates. MongoDB indexes only the first occurrence of the key and deletes all documents from the Collection that contain the next occurrence of that key. Specify true to create dropDups index. The default value is false sparse Boolean If true, the index only references the Documents with the specified field. These indexes use less space, but operate in different ways in some situations (specifically, sorting). The default value is false expireAfterSeconds Integer Defines a value (in seconds), in the form of a TTL to control how long MongoDB maintains Document in this Collection v Version index Version number of the index. The default version depends on the version of MongoDB running when creating the weights document index A number in the range from 1 to 99999 and denoting the meaning of the relational field to other indexed fields in terms of score default_language String With a text index, this is the language that defines the rules of the index. The default value is english language_override String With a text index, specify the name of the field contained in the Document, the language to load the default language. The default value is language

According to Tutorialspoint

Previous post: Arrange records in MongoDB

Next lesson: Aggregation in MongoDB

5 ★ | 1 Vote