MongoDB Index

Ratings:
(4)
Views: 0
Banner-Img
Share this blog:

Indexes can support the efficient execution of queries. Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect. Use the create index() method to create an index on a collection. Indexes can support the efficient execution of queries. MongoDB automatically creates an index on the _id field upon the creation of a collection. To create an index on a field or fields, pass to the create index()method an index key specification document that lists the fields to index and the index type for each field:

MongoDB Index Types

MongoDB provides different types of indexes to support specific types of data and queries.

Default _id

All the collections of MongoDB have an index on the _id field that exists by default. If no values specified for the _id the MongoDB will create an _id field with an ObjectId value. The _id index is unique, it restricts clients from inserting two documents with the same value for the _id field.

Single Field

MongoDB supports user-defined indexes on a single field of a document. Here is the example below considering a single-column index.

db.userdetails.find().sort({"education":1})

The above example shows the "education" column from the collection of user details is sorting in ascending order.

Compound Index

MongoDB also supports user-defined indexes in multiple fields. The order of fields listed in a compound index has significance. For instance, if a compound index consists of {"education": 1, "password": -1 }, the index sorts first by education and then, within each education value, sort by a password. Consider the following illustration of this compound index:

db.userdetails.find().sort({"education":1,"password":-1})

Multikey Index

MongoDB uses multikey indexes to index the content stored in arrays. When you index on a column that holds an array value, MongoDB creates separate index entries for every element of the array. These multikey indexes allow queries to select documents that contain arrays by matching on element or elements of the arrays. Here is a sample document within a collection:

{
        "_id" : ObjectId("528f34950fe5e6467e58ae77"),
        "user_id" : "user1",
        "password" : "1a2b3c",
        "sex" : "Male",
        "age" : 17,
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "extra" : {
                "friends" : {
                        "valued_friends_id" : [
                                "kumar",
                                "harry",
                                "anand"
                        ],
                        "ban_friends_id" : [
                                "Amir",
                                "Raja",
                                "mont"
                        ]
                }
        }
}

Consider the following illustration of a multikey index:
db.userdetails.find().sort({"extra.friends.ban_friends_id":1})

Here in the above example, the 'extra' is an array, and 'friends' and 'ban_firends_id' are subarrays.

Geospatial Index

To support efficient queries of geospatial coordinate data, MongoDB provides two special indexes: 2d indexes and 2sphere indexes uses for planar geometry when returning results spherical geometry to return results.

Text Indexes

Another type of index that MongoDB provides is text index, which supports searching for string content in a collection. These text indexes do not store language-specific stop words (e.g. “the”, “a”, “or”) and restrict the words in a collection to only store root words.

Hashed Indexes

To support hash-based sharding, MongoDB provides a hashed index type, which indexes the hash of the value of a field. These indexes have a more random distribution of values along with their range, but only support equality matches and cannot support range-based queries.

Index Properties

Unique Indexes

The unique property for an index causes rejects duplicate values for the indexed field. To create a unique index on a field that already has duplicate values. Other than the unique constraint, unique indexes are functionally interchangeable with other MongoDB indexes.

Sparse Indexes

The sparse property of an index ensures that the index only contains entries for documents that have the indexed field and skips documents that do not have the indexed field. The sparse index option can be combined with the unique index option to reject documents that have duplicate values for a field but ignore that do not have the indexed key.

TTL Indexes

Another special index is TTL indexes that can be used to automatically remove documents from a collection after a certain amount of time. This is ideal for certain types of information like machine generated event data, logs, and session information that only need to stay behind in a database for a finite amount of time.

MongoDB Single Field Indexes

All the indexes in MongoDB are B-tree indexes, which supports equality matches and range queries. Internally the index arranged items in an order, sorted by the value of the index field. The ordering of the index allows MongoDB to return sorted results using the order of documents in the index. MongoDB provides indexes on any field in a collection of documents. MongoDB supports either a single field or multiple fields depending on the operations a user needs. The default index for collections have MongoDB on the _id field, the additional indexes can be added by the user to support important queries and operations. Here is the documents in the collection 'empinfo'.

{
        "_id" : ObjectId("548eb3a051ca033c1c01054d"),
        "emp_id" : "SALS",
        "emp_name" : "Pat",
        "designation" : "clerk",
        "department" : "sales"
}
{
        "_id" : ObjectId("548eb3a051ca033c1c01054e"),
        "emp_id" : "OFFC",
        "emp_name" : "Jons",
        "designation" : "peon",
        "department" : "sales"
}
{
        "_id" : ObjectId("548eb3a051ca033c1c01054f"),
        "emp_id" : "CLC",
        "emp_name" : "Tnit",
        "designation" : "president",
        "department" : "sales"
}
The following command create the index on emp_id column.

> db.empinfo.ensureIndex( { "emp_id" : 1 } );
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

If you want to see the list of  created index, here is the following command.

> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "empinfo.empinfo" }
{ "v" : 1, "key" : { "emp_id" : 1 }, "name" : "emp_id_1", "ns" : "empinfo.empinfo" }

Indexes on Embedded Fields

The indexes can be created on fields embedded in sub-documents, as you can index top-level fields in documents. There is a difference between Indexes on embedded fields and indexes on sub-documents. The indexes on subdocument include the full content up to the maximum index size where as indexes on embedded fields allow the user to use a “dot notation,” to self-searching into sub-documents.

Aspired to become an Mongo DB?
Explore the post to discover the know-hows on Mongo DB Training.

Consider the following collection named 'empinfo' that contains the documents.

{
        "_id" : ObjectId("548fe6d898fd7de25e8d3aa9"),
        "emp_id" : "SALS",
        "emp_name" : "Pat",
        "address" : [
                {
                        "street" : "Court Street",
                        "city" : "London",
                        "zip" : "5698722"
                }
        ]
}
{
        "_id" : ObjectId("548fe6d898fd7de25e8d3aaa"),
        "emp_id" : "OFFC",
        "emp_name" : "Jones",
        "address" : [
                {
                        "street" : "Church Avenue",
                        "city" : "Paris",
                        "zip" : "632902"
                }
        ]
}

The following command create the index on address.zip  field, using the following specification:
> db.empinfo.ensureIndex( { "address.zip": 1 } );
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

If you want to see the created index, see the following command.


> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "empinfo.empinfo" }
{ "v" : 1, "key" : { "address.zip" : 1 }, "name" : "address.zip_1", "ns" : "empinfo.empinfo" }
>

Indexes on Subdocuments

An index can also be created in the subdocument. Here in the below, a sample collection named 'empinfo' contain a field address within its documents.

{
        "_id" : ObjectId("54901b4b4b71cc67737765a5"),
        "emp_id" : "SALS",
        "emp_name" : "Pat",
        "address" : {
                "street" : "Court Street",
                "city" : "London",
                "zip" : "5698722"
        }
}
{
        "_id" : ObjectId("54901b4b4b71cc67737765a6"),
        "emp_id" : "MARK",
        "emp_name" : "James",
        "address" : {
                "street" : "Church Avenue",
                "city" : "Paris",
                "zip" : "532460"
        }
}


The address field is a subdocument, containing the embedded fields city and zip. The following command creates an index on the address  field as a whole.
> db.empinfo.ensureIndex( { address: 1 } );
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

If you want to lists the creating index, here is  the command below.

> db.system.indexes.find();
{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "empinfo.empinfo" }
{ "v" : 1, "key" : { "address" : 1 }, "name" : "address_1", "ns" : "empinfo.empinfo" }

The following query can use the index on the address  field :
> db.empinfo.find( { address: { street: "Court Street", city: "London",zip:"5698722" } } ).pretty();

{
        "_id" : ObjectId("54901b4b4b71cc67737765a5"),
        "emp_id" : "SALS",
        "emp_name" : "Pat",
        "address" : {
                "street" : "Court Street",
                "city" : "London",
                "zip" : "5698722"
        }
}

From the above output it seems that, when performing equality matches on subdocuments, field order matters and the subdocuments must match exactly. See the example below, the following query does not match the above document:

db.empinfo.find( { address: { city: "London",zip:"5698722",street: "Court Street" } } );

For indepth understanding click on

You liked the article?

Like: 0

Vote for difficulty

Current difficulty (Avg): Medium

EasyMediumHardDifficultExpert
IMPROVE ARTICLEReport Issue

About Author

Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.

Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox