In MongoDB, you can create an index on a collection using the createIndex()
method, the createIndexes()
method, or the createIndexes
administration command.
The createIndex()
Method
The db.collection.createIndex()
method creates an index on the specified collection. It’s a wrapper for the createIndexes
administration command.
Example:
db.pets.createIndex( { weight: -1 } )
This creates an index on the pets collection. It’s a descending index on the weight
field. We know it’s descending because we specified -1
. If we’d specified 1
it would have been ascending.
A compound index is one that’s defined on multiple fields. You can create a compound index by separating each field with a comma.
Example:
db.pets.createIndex( { name: 1, type: -1 } )
This creates a compound index on the name
field (in ascending order) and the type
field (in descending order).
The createIndexes()
Method
The db.collection.createIndexes()
method creates one or more indexes on a collection This method is also a wrapper for the createIndexes
administration command.
When you create indexes with the createIndexes()
method, you need to provide the fields in an array. You need to do this even if you’re only creating a single index.
Example:
db.pets.createIndexes( [ { weight: -1 } ] )
That does the same thing that the first example does. It creates a descending index on the weight
field.
To create multiple indexes, separate each index’s document by a comma.
Example:
db.pets.createIndexes( [ { name: 1 }, { weight: -1 } ] )
You can also create compound indexes with createIndexes()
. To do this, simply define the compound index within the document for that index.
Therefore, we could do the following:
db.pets.createIndexes( [ { name: 1, type: -1 }, { weight: -1 } ] )
That creates a compound index for the name
and type
fields, and a separate index for the weight
field.
The createIndexes
Command
The createIndexes
administration command creates one or more indexes on a collection The previous two methods are wrappers around this command.
Therefore, we can use the createIndexes
command to create the indexes that we did in the previous examples.
Here’s an example of creating an index on the name
field:
db.runCommand(
{
createIndexes: "pets",
indexes: [
{
key: { "name" : 1 },
name: "idx_name_1"
}
]
}
)
In the previous examples we let MongoDB name our indexes, but in this case we named the index idx_name_1
.
Here’s an example of creating multiple indexes:
db.runCommand(
{
createIndexes: "pets",
indexes: [
{
key: { "name" : 1, type: -1 },
name: "idx_name_1_type_-1"
},
{
key: { "weight" : -1 },
name: "idx_weight_-1"
}
]
}
)
In this case, the first index is a compound index on the name
and type
fields, and we named it idx_name_1_type_-1
.
The second index is on the weight
field and we called it idx_weight_-1
.
Wildcard Indexes
All three of the above approaches support wildcard indexes as of MongoDB 4.2 (the featureCompatibilityVersion
must be at least 4.2
to create wildcard indexes).
Wildcard indexes are useful for collections that contain unstructured data with different fields in different hierarchies.
See How to Create a Wildcard Index in MongoDB for examples.
MongoDB Documentation
Below are links to the MongoDB documentation for each of the above: