MongoDB Lab5: Indexes
Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and require MongoDB to process a large volume of data.
Indexes are special data structures, that store a small portion of the data set in an easy-to-traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in the index.
The createIndex() Method
To create an index, you need to use createIndex() method of MongoDB.
Syntax
The basic syntax of createIndex() method is as follows().
>db.COLLECTION_NAME.createIndex({name:1})
Here key is the name of the field on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1.
Example
>db.mycol.createIndex({"title":1,"description':-1})
use >db.col.getIndexes() to list out all Indexes in the collection
The dropIndex() method
You can drop a particular index using the dropIndex() method of MongoDB.
Syntax
The basic syntax of DropIndex() method is as follows().
>db.COLLECTION_NAME.dropIndex({name:1})
Comments
Post a Comment