Best e-commerce development company. Best app development company. Best designing company. Best service provider globally.

Indexing in MongoDB.

Indexing in a database is a technique used to speed up data lookup from tables or collections. Without indexing, it may take some time to find an entry. For instance, if a table contains thousands or more entries without indexing, finding the data typically necessitates going through each row. In indexing one or more columns can be used to generate indexes and stored in an ordered manner. Which results in quick and efficient lookup of data.
MongoDB has an index for the _id field by default. In addition to the default index, a user can also define single field indexes and multiple fields. These indexes are created using the keyword ’createIndex’.
To show all indexes in a collection use the command ‘db.collectionName.getIndexes()’
To drop an index from a collection use the command ‘db.collectionName.dropIndexes(fieldName:type)’

Types of Indexes in MongoDB

Single Field Index

To create an index on a single field in a collection, use single field index
Syntax : db.collectionName.createIndex({fieldName:1})
Example : db.salesInventory.createIndex({item:1})
In the above example the collection name is salesInventory and item is the field inside that collection. The number 1 indicates the sort order .Sorting in ascending order is denoted by 1 ,whereas -1 indicates sorting in descending order.

Compound Index

Compound indexes are indexes that contain multiple fields.
Syntax: db.collectionName. createIndex({filed1:type, field2:type, …..})
Example : db.employee. createIndex({empName:1, empDept:-1})
In this illustration, the employee collection’s files empName and empDept are indexed.
empName in ascending order and empDept in descending order.

Multikey Index

To use indexing for an array field in a collection use multikey indexing.
Syntax : db. CollectionName. createIndex({arrayName:arrayKey:type})
Example : db. examResults. createIndex({evaluationData:markScored:1})
Collection Example : examResult { id:ObjectId(“64548f497fae9e42e9013f68”),
name:Jane
class:12
evaluationData:{
{subject:”English”,markScored:”86”,maxMark:”100”},
{subject:”Science”,markScored:”92”,maxMark:”100”},
}
}
In the above example evaluationData is a collection filed in array format.

For more MongoDB services reach out to Sreyas, we provide our service, support, and maintenance around Europe and worldwide.

Recent Blogs


Posted

in

,

by

Tags:

To Know Us Better

Browse through our work.

Explore The Technology Used

Learn about the cutting-edge technology and techniques we use to create innovative software solutions.