MongoDB Index & Index Types

MongoDB Index & Index Types 



Introduction

MongoDB, a powerful NoSQL database, is designed for scalability, high availability, and performance from single server deployments to large complex multi-site architectures. One of the essential features of MongoDB that helps in achieving these goals is the Index.

What is an Index?

An index in MongoDB is a special data structure that holds the data of few fields of documents on which the index is created. Indexes improve the performance of search operations in the database as they provide a more efficient path to the data. Just as an index in a book helps you locate content quickly, a MongoDB index allows the database to find and retrieve specific documents much faster than it would without an index.

How to create an index:-

Now that we have an understanding of what indexes are and when to use them, we can get into the method of creating one.

Once you have identified a field that could benefit from indexing, you use the createIndex() method of MongoDB. The basic syntax is as follows:

db.COLLECTION_NAME.createIndex( { "FIELD_NAME": 1 } )

FIELD_NAME is the name of the field that you want to create the index on, and 1 dictates an ascending order.

E.g:- We are going to create collection,Collection name is createindex and fieldname is name and after that we are going to create index. given below snapshot


After that we are going to create index single column,given below command:-

 db.createindex.createIndex( { "name": 1 } );



Create index in multiple column

You can also create an index on multiple field with the createIndex() method by creating a comma seperated list like the following:

db.COLLECTION_NAME.createIndex( { "FIELD_NAME_1": 1, "FIELD_NAME_2": -1 } );


How to show indexes

Once you have started creating indexes, you may want to check what indexes exist on your database instances. In MongoDB, you can use the getIndexes() method to return descriptions of all the indexes in a collection.

The basic syntax for seeing all of your collection's indexes is:

db.COLLECTION_NAME.getIndexes();


Drop Index:-

In order to drop an index, MongoDB provides the dropIndex() method. 

Syntax:-

db.NAME_OF_COLLECTION.dropIndex({KEY:1}) 

In this snapshort first we have show the index and after that drop the index and again we have check check the index in collection.



The dropIndex() methods can only delete one index at a time. In order to delete (or drop) multiple indexes from the collection, MongoDB provides the dropIndexes() method that takes multiple indexes as its parameters. 

Syntax:-

db.NAME_OF_COLLECTION.dropIndexes({KEY1:1, KEY2: 1}) 


The dropIndex() methods can only delete one index at a time. In order to delete (or drop) multiple indexes from the collection, MongoDB provides the dropIndexes() method that takes multiple indexes as its parameters. 

MongoDB indexing types:

MongoDB provides different types of indexes that are used according to the data type or queries. The indexes supported by MongoDB is are as follows:

There are several types of indexes in MongoDB that cater to different types of queries:

  • Single Field Index: As the name suggests, this index is on a single field of a document.
  • Compound Index: This is an index that is on multiple fields of a document. The order of fields in the index matters as it determines the sort order.
  • Multikey Index: This index is used when you have an array of values in a field. MongoDB creates separate index entries for every element of the array.
  • Text Index: This index is used for text search. It tokenizes the string in the field and indexes each token.
  • Geospatial Index: This index is used for geospatial data.
  • wildcard indexes:Use wildcard indexes to support queries against arbitrary or unknown fields.
Single Field Index:-

The simplest type of indexing is single field which you can index a single key from a document like the example above in the picture.

it stores information from a single field in a collection. By default, all collections have an index on the _id field. You can add additional indexes to speed up important queries and operations.

db.collection_name.createIndex({fieldname: 1})

  • Number 1, indicating ascending order. this means that the data placed in the index list will be arranged in ascending order.

  • Number -1, indicating descending order. this means that the data placed in the index list will be arranged in descending order.

e.g:-

First checked the index avilable or not in this collection ,using given below command:-

db.singlefieldindex.getIndexes()


now going to create index.

db.singlefieldindex.createIndex( { "name": 1 } );


finally we have checked the index createor not,using given below command:-

db.singlefieldindex.getIndexes()


Compound Index:-

We can combine multiple fields for compound indexing and that will help for searching or filtering documents in that way. Or in other words, the compound index is an index where a single index structure holds multiple references.

Syntax:

db.<collection>.createIndex( { <field1>: <type>, <field2>: <type2>, … } )

Here, we can combine the required fields in this pattern. Also the value of these fields is 1(for ascending order) or -1(for descending order).

e.g;-


Note: Compound indexes may have a single hashed index field but a hashing function is required by Hashed indexes to compute the hash of the value of the index field.

Multikey Index:

MongoDB uses the multikey indexes to index the values stored in arrays. When we index a field that holds an array value then MongoDB automatically creates a separate index of each and every value present in that array. Using these multikey indexes we can easily find a document that contains an array by matching the items. In MongoDB, you don’t need to explicitly specify the multikey index because MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value. 

Syntax:

db.<collection>.createIndex( { <field>: <type>} )

Here, the value of the field is 1(for ascending order) or -1(for descending order).

e.g:-


Text Index:

Text index is a type of index that enables full-text search on string content within documents. It allows you to perform text search queries efficiently, enabling features like keyword search, phrase search, and natural language processing.

You can create a text index on the “title” field like this:

db.<collection>.createIndex( { <field>: “text”} )

can give exact phrases also for searching by enclosing the search terms in double quotes

db.<collectionname>.find( { $text: { $search: “\”<Exact search term>\”” } } ) 


Hashed Index:

When you create a hashed index on a field, MongoDB hashes the values of that field and stores the hashed values in the index. This makes queries that match on the hashed field fast, as MongoDB only needs to perform a hash computation on the query value to find matching documents.

This kind of index is mainly required in the even distribution of data via sharding. Hashed keys are helpful to partition the data across the sharded cluster.

Syntax:

db.<collection>.createIndex( { _id: “hashed” } )




Comments

Popular posts from this blog

MongoDB Architecture

MySQL Replication Switchover: Step-by-Step Guide

Mysql Commercial Mysqlbackup:How to take Differential or Incremental Backup and resotre using mysqlbackup utility in mysql 8.0.37 enterprise edition