Indexes in MongoDB
Index: Index is a data structure that enhances the data retrieval efficiency by storing the small portion of the collection’s data along with the pointers to their physical location in the collection.
👉 MongoDB indexes use B-tree data structure.
👉 Indexes help to access the data that you want rather than scanning all the data and finding it. This will improve the query performance.
👉 In MongoDB every collection has one index by default on _id field.
👉 In MongoDB index creation will take two parameters one is the field name and the other is the field sorting order.
Pros of using indexes:
1. Fast search i.e., improves Query performance.
2. Efficient sorting.
3. Efficient aggregation.
4. Reduces Disk space.
Cons of using indexes:
1. Consumes more storage.
2. Slowdown writing operations.
3. Increases more memory usage.
Type of Indexes:
1. Single field index
2. Compound indexes
3. Multi key indexes
4. Text indexes
5. Hashed indexes
1. Single field index
👉Type of index that is created on a single field within a document in the collection.
👉 It is the most commonly used index.
Syntax:
1 |
db.collection_name.createIndex( { Field_name : sorting_order } ) |
Field_name: Field name on which you want to create an index.
sorting_order: If you give “1” the fields will be sorted in ascending order.
If you give “-1” the fields will be sorted in descending order.
Ex: If you want to create an index on field name called marks in descending order.
1 |
db.collection_name.createIndex({ marks : -1}) |
2.Compound indexes
👉 Type of index that is created on multiple fields within a document in a collection.
👉 Used to optimize the query performance for queries which involve a combination of fields.
Syntax:
1 |
db.collection_name.createIndex( { Field_name : sorting_order, Field_name2 : sorting_order,Field_name3 : sorting_order, ....} ) |
Ex: If you want to create an index on field name called marks in descending order and name in ascending order.
1 |
db.collection_name.createIndex({ marks : -1 , name : 1 }) |
3.Multi key indexes
👉 Type of index that is created on an array field within a document in a collection.
👉 Used to create indexes on the values within the array field in the document.
Syntax:
1 |
db.collection_name.createIndex( { ArrayField_name : sorting_order } ) |
Ex: If you want to create an index on field name called scores in ascending order.
1 |
db.collection_name.createIndex({ scores : 1}) |
4.Text indexes
👉 Type of index that is created on a text field which is used to search the text within the documents.
👉 Used when you want to search for documents which contain specific words.
Syntax:
1 |
db.collection_name.createIndex( { Field_name : “text” } ) |
Instead of the sorting order we give text as the parameter.
Ex: If you want to create a text index on a field called name.
1 |
db.collection_name.createIndex({ name : “text” }) |
5.Hashed indexes
👉 Type of index that is majorly used for fields with high cardinality i.e., large and unique values.
👉 Hashed indexes use the hash function to map the field values to a fixed number of buckets which also helps in sorting the field.
Syntax:
1 |
db.collection_name.createIndex( { Field_name : “hashed” } ) |
Instead of the sorting order we give hashed as the parameter.
Ex: If you want to create a hashed index on a field name called SSN_ID.
1 |
db.collection_name.createIndex({ SSN_ID : “hashed” }) |
Author : Teja |
LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Note: Please test scripts in Non Prod before trying in Production.