How to Create Indexes in MongoDB

Shraddha Paghdar Feb 02, 2024
How to Create Indexes in MongoDB

Indexes assist in the effective resolution of queries. Without indexes, MongoDB has to go through each document in a collection to find the ones that match the query.

It can waste time and requires MongoDB to handle such information. So, in today’s article, we’ll learn how to create indexes in MongoDB.

Create an Index in MongoDB

Indexes are specialized data structures that keep a limited subset of the dataset in an accessible format. The value of a particular field or collection of fields is stored in the index, ordered by the field’s value as indicated in the index.

MongoDB supports different types of indexes. Single Field, Compound Index, Multikey Index, Geospatial Index, Hashed Index, and Clustered Index are some of the indexes.

Syntax:

db.COLLECTION.createIndex({ KEY_NAME:1 })
  1. KEY_NAME is the field name on which you wish to establish an index.
  2. The previous point indicates that the order should be ascending. Therefore, you must use -1 to construct an index in descending order.
  3. The createIndex() method accepts a list of optional arguments. The listing is below.
    • unique is a Boolean variable that creates a unique index, preventing the collection from accepting the insertion of documents whose index key or keys already exist. To establish a unique index, enter true. The default value is set to false.
    • The Boolean variable background builds the index in the background, so it does not obstruct other database operations. To construct in the background, specify true. The default value is set to false.
    • If the Boolean variable sparse is set to true, the index will only refer to documents with the specified field. Although these indexes take up less space, they can act differently (particularly sorts). false is the default setting.
    • The string variable name contains the index’s name. If an index name isn’t supplied, MongoDB creates one by joining the names of the indexed fields and the sort order.
    • The string variable default language is the language that establishes the list of stop words and the criteria for the stemmer and tokenizer for a text index.
    • The integer variable expireAfterSeconds defines a time-to-live (TTL) value in seconds to limit how long MongoDB keeps the documents in this collection.

You can find more information about the .createIndex() here. Let’s use the following example to understand the mentioned idea:

> db.users.createIndex({ "email": 1, "country": -1 })

In the above example, we made a new compound index where email is stored in descending order, and country is sorted in ascending order. Every time a query is executed, the index first sorts by user’s email before sorting by country within each email value.

Shraddha Paghdar avatar Shraddha Paghdar avatar

Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.

LinkedIn

Related Article - MongoDB Index