MongoDB Create Index on collection

By Yashwant Chavan, Views 1895, Date 29-Oct-2016

In this tutorial we will learn how to create Index on collection using mongodb db.collection.createIndex() method.

tags mongodb

Creates index syntax

db.collection.createIndex(keys, options);

keys - A document in the form of key and value pairs where the field is the index key and the value describes the type of index for that field. For an ascending index on a field, specify a value of 1; for descending index, specify a value of -1.

options - Optional document type parameter, A document that contains a set of options that controls the creation of the index.

Options for All Index Types

background - Optional boolean type parameter, Some times index creation activity takes long time to build the index. So this parameter helps to builds the index in the background, which does not block other database activities. Specify true to build in the background.

unique - Optonal boolean type parameter, Specify true to create a unique index. The default value is false.

name - Optional string type parameter, The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.

partialFilterExpression - Optional document type parameter, If specified, the index only references documents that match the filter expression.

sparse - Optional boolean type parameter, If true, the index only references documents with the specified field.

expireAfterSeconds - Optional integer type parameter, Specifies a value, in seconds, as a TTL (time to live) to control how long MongoDB retains documents in this collection.

storageEngine - Optional document type parameter, Allows users to specify configuration to the storage engine on a per-index basis when creating an index.

storageEngine - Optional document type parameter, Allows users to specify configuration to the storage engine on a per-index basis when creating an index.

Create an Ascending Index on a Single Field

creates an ascending index on the field creation_date field. You can creates index on more than one fields using compound index.

db.articles.createIndex( { creation_date: 1 } )

output

> db.articles.createIndex( { creation_date: 1 } )
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

Create an Index on a Multiple Fields

The following example creates a compound index on the title field (in ascending order) and the creation_date field (in descending order.)

db.articles.createIndex( { title: 1, creation_date: -1 } )

output

> db.articles.createIndex( { title: 1, creation_date: -1 } )
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}
>
Yashwant Chavan

Yashwant Chavan

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan@gmail.com