Working with Index in MongoDB

In every database management system indexing is an essential attribute. Without indexing the system has to search the whole database in case of every queries. Which can prove very problematic. And in case of mongodb where we use blob data, can be disastrous with that much data. So, in this article we are going to discuss about indexes.

What is index in mongodb?

Index in mongodb is a special data structure that store a small portion of the collection’s data set in an easy to traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field. Index supports the efficient execution of queries in mongodb.

To implement index we need to use a method called ensureIndex(). Let us work through a  basic query to implement index:


db.collection_name.ensureIndex({KEY:1})

In the above query, ‘KEY’ is the name of field on which index should be created and 1 is for ascending order. To create index in descending order use -1.

ensureIndex() also supports many option,although those are optional, we should have a basic idea.

Background:
Background builds the index in the background so that building an index does not block other database activities. The default value is false.For example the query looks like


db.collection.ensureIndex( { a: 1 }, { background: true } )

Unique:
Unique creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing value in the index. For example the query looks like


db.collection.ensureIndex( { a: 1 }, { unique: true } )

Drop Duplicates:
Mongodb cannot create a unique index on a field that has duplicate values. To forcefully create the unique index, dropDups option can help us. Which will only index the first occurrence of a value for the key, and delete all subsequent values. The basic query looks like


db.accounts.ensureIndex( { username: 1 }, { unique: true, dropDups: true } )

Name:
The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.For example the query looks like


db.collection.ensureIndex(
   {
     content: "text",
     "users.comments": "text"
   },
   {
     name: "MyTextIndex"
   }
)

Sparse:
These indexes use less space but behave differently in some situations. The default value is false.
For example the query looks like


db.collection.ensureIndex( { a: 1 }, { sparse: true } )

Hashed:
Hashed indexes compute a hash of the value of a field in a collection and index the hashed value. These indexes permit equality queries. Mongodb supports hashed indexes to any single field. The hashing function collapses sub-documents and computes the hash for the entire value. But it is unable to support multi-key indexes. The basic query for creating a hashed index is:


db.collection.ensureIndex( { _id: "hashed" } ) 

Compound:
Mongodb supports compound indexes, where a single index structure holds references to multiple fields within a collection’s documents. Compound indexes can support queries that match on multiple fields. Let us view the basic query:


db.collection.ensureIndex( { a: 1, b: 1, c: 1 } )

Index Intersection in MongoDB:

Index intersection involves the use of multiple indexes to satisfy a query. In shorter terms, each index intersection involves two indexes. But mongdb can employ multiple index intersections to resolve a query. As a basic query we can look at:


db.collection.find( { a: "abc123", i: { $gt: 15 } } )

Mongodb can use an intersection of either the entire index or the index prefix. Index prefix is a subset of a compound index. It consists of one or more keys starting from the beginning of the index. In the following query which specifies a condition on both the q field and the s field, mongodb can use the intersection of the two indexes:


db.collection.find( { q: { $gt: 10 } , s: "A" } )

Difference between compound index and index intersection:

Compound Index

Index Intersection

compound indexes are indexes on multiple fields.

Index intersection involves the use of multiple indexes to satisfy a query.

Index intersection provides flexibility if there isn’t a suitable compound index to cover all of the common queries.

compound index may be more selective and be able to find results inspecting fewer index entries.

Although we have discussed about the basics of indexing in this article. There are still place for the growth. We can use index in many way to make our quarries more efficient.

Related Links:

1> How to Get Started with MongoDB Database?
2> How to Get Started with MongoDB?
3> How to Import and Export Through Mongodb?
4> How to Use Projection in MongoDB?
5> Using sort method in mongodb
6> Map-Reduce in MongoDB
7> Introduction to Replication in MongoDB
8> Deploying a Replica Set in MongoDB
9> Discussing Replication Lag in MongoDB
10> Replica Set Members in Mongodb
11> Working with Sharding in MongoDB
12> Working with Aggregation in MongoDB
13> How to Work with Aggregation Framework in MongoDB?
14> Working with Pipeline Concept in MongoDB
15> Discussing about Pipeline Expression in MongoDB

If you find this article helpful, you can connect us in Google+ and Twitter.

3 thoughts on “Working with Index in MongoDB

Leave a Reply

Your email address will not be published. Required fields are marked *