Collection Indexing In MongoDB

Posted By : Vipin Pokhriyal | 29-Mar-2018

In Database, indexes are a data structure that improves the performance of data retrieval from data tables. Like other databases, MongoDB also uses indexing for faster data retrieval and efficient execution of queries. Without indexing, MongoDB needs to scan the whole collection objects to fetch or retrieve the query statement.

In MongoDB, indexes define at the collection level and can support index at any field or subfield of the document in MongoDB Collections.

For Example :

 

 

db.users.find({score:{$lt:30}).sort({score:-1})

 

 

In the above query select the score less than 30 and order then using score index here score=1 is an index.

 

Default _id index in MongoDB:

MongoDB creates a default index with name _id during the creation of the collection. The _id index is used to prevent from inserting any two documents with the similar value for the _id field. We can not drop the index on the _id field.

 

 

 

Creating an Index:

Syntax to create Index in mongoDB:

db.users.createIndex({<key and Index type specification>},function(err,result){
cosole.log(result);
callback(result);
})

 

 

For Example:

db.users.createIndex({ name:1},function(err, result){
console.log(result);
callback(result);
})

 

 

The above query used to create an index on the name of user collection in ascending orders.

MongoDB creates an index on the field if the index is not already implemented on that field.

 

 

 

Single Field Index:

Other than using default index _id in MongoDB, it provides support for the creation of user-defined ascending/descending on a single field.

For Example:

db.users.createIndex({ name: -1}),function(err, result){
console.log(err, result);
callback(result);

 

Compound Index

MongoDB also provides support to create the index on multiple fields on the same collection.

db.users.createIndex({ name: -1, score:1}),function(err, result){
console.log(err, result);
callback(result);

 

 

Here in user collection index on name field has been used in descending order and on score field ascending order.

 

 

 

Multikey Index

In mongoDB , multikey list is used the data stored in arrays. If array cheerful is indexed previously mongoDB sew seperate roster entries for each factor of array.

db.user.createIndex({ address.zip:1},function(err,result){
console.log(err,result);
callback(result);
})

 

 

 

Getting index specification on a collection:

db.collections.getIndexes();

 

 

returns an array of docs that holds information about the indexes of the collection.

 

 

 

Dropping the index of collection:

collection.dropIndex();

 

 

dropIndex, removes or drops implemented index of a collection.

 

Index of any field in collection can be dropped in two ways :

 

 

1 – Can use index name to drop index of collection.

db.users.dropIndex('userIDx');

 

 

where userIDx is the index name on userId field of user collection.

 

 

2- Can use index key and value specified document.

db.users.dropIndex({userId:1},function(err, result){
console.log(err,result);
callback(result);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

About Author

Author Image
Vipin Pokhriyal

Vipin is Qualified in Master in computer Applicatins. He is an Active team player & having skills as Java Developer.

Request for Proposal

Name is required

Comment is required

Sending message..