Indexes

  • In a database, a small portions of collection's data is stored in an easy traversable form in a special data structure. This data structure is called index, which make querying faster.

Creating an Index

  • Syntax

    db.collection_name.createIndex({"field Name":1})

    • The above command creates an index on birth year field in increasing order of birth year.

Creating a compound index

  • These are indexes created on multiple fields.

  • Syntax

    db.collection_name.createIndex({"field Name1": 1, "field Name2": 1})

Getting an index of collection

  • To get index created on collection can be retrieved as follows,

  • Syntax

    db.collection_name.getIndexes()

Last updated