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 })
KEY_NAME
is the field name on which you wish to establish an index.- The previous point indicates that the order should be ascending. Therefore, you must use
-1
to construct an index in descending order. - 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, entertrue
. The default value is set tofalse
.- The Boolean variable
background
builds the index in the background, so it does not obstruct other database operations. To construct in the background, specifytrue
. The default value is set tofalse
. - If the Boolean variable
sparse
is set totrue
, 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 thestemmer
andtokenizer
for a text index. - The integer variable
expireAfterSeconds
defines atime-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 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