createIndexes() – Creates one or more indexes on a collection

Creates one or more indexes on a collection.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. createIndexes()({indexes})

Command parameters

indexes
keyPatterns : An array containing index specification documents.
  • Each document contains field and value pairs, where the field is the index key and the value describes the type of index for that field.
  • Syntax:
    {<index key> : -1|1}
    where 1 specifies an index in ascending order and -1 specifies an index in descending order.
.

Examples

The following example shows the command syntax for creating a series of indexes for the collection, members:
db.members.createIndexes([ { score : 1 }, { price : 1 }, { task : 1 }, { category : 1 } ])
The following example shows the output returned from running the createIndexes command:
{
    “indexName1” : “members_xscore”,
    “indexName2” : “members_xprice”,
    “indexName3” : “members_xtask”,
    “indexName4” : “members_xcategory”
}