What is Indexing? Types of Mongo DB Indexing
Indexing:-
Indexing is very important for improving the performance of search queries and reducing the database load. When we continuously perform searches in a document, we should index those fields that match our search conditions.
For any kind of database, indexes are of great importance. In MongoDB, we can index any field with primary and secondary indices. Making query searches faster, MongoDB indexing enhances the performance of the database.
Indexes are special data structures used to locate the record in the given collection very quickly without being required to traverse through every record in the document.MongoDB uses these indexes to limit the number of documents that have to be searched in a collection.
Default Index
In MongoDB indexing, all the collections have a mongoose object id and it is the default index on the _id field. If we don’t specify any value for the _id the MongoDB will create _id field with an object value automatically. This index prevents clients from creating duplicate documents with the same _id.
createIndex()
To create an index, you need to use createIndex() method which will create an index for the specified field.
Syntax
db.COLLECTION_NAME.createIndex({KEY:1})
dropIndex()
Syntax
The syntax of DropIndex() method is as follows().
db.COLLECTION_NAME.dropIndex({KEY:1})
getIndexes()
This method returns the description of all the indexes into the collection.
Syntax
Basic syntax for the getIndexes() method
db.COLLECTION_NAME.getIndexes()
Types of MongoDB indexings:-
Mongo DB supports different types of indexings which are listed below.
Single Field Index
MongoDB supports user-defined indexes like single field indexes for indexing. A single field index is used to create an index on the single field of a document that is present in the collection. With a single field index, MongoDB can traverse in ascending and descending order collection documents.
Compound Index
MongoDB also supports user-defined indexes on multiple fields as well. For this MongoDB has a compound index. There is a sequential order of fields for a compound index in MongoDB.
For example, if a compound index consists of {“name”:1,”city”:1}), then the index will sort first the name and then the city. This is called as compound indexing in MongoDB.
db.dataflair1.find().sort({“name”:1,”city”:1})
{ “_id” : ObjectId(“Sadfeb8931cffc950c924471”), “name” : “amit”, “city” : “hyd” }
{ “_id” : ObjectId(“Sadfeb6e31cffc950c92446f”), “name” : “ankitt”, “city” : “hyd” }
{ “_id” : ObjectId(“Sadfeb7c31cffc950c924470”), “name” : “shiva”, “city” : “ujjain” }
Multikey Index
MongoDB uses the multikey indexes to index the values stored in array form. If we index a field with an array value, It creates separate index entries for each element of the array. These indexes allow queries to select documents with the matching criteria compared with the arrays.
Below is the example for the multikey index.
db.dataflair1.find().sort({“name”:1,”city”:1})
{ “_id” : ObjectId(“Sadfeb6e31cffc950c92446f”), “name” : “lalit”, “city” : “indore” }
{ “_id” : ObjectId(“Sadfeb7c31cffc950c924470”), “name” : “rohini”, “city” : “ujjain” }
{ “_id” : ObjectId(“Sadfeb8931cffc950c924471”), “name” : “shiva”, “city” : “ujjain” }