When you create an index in MongoDB, if you don’t specify a name for it, MongoDB will assign one.
The default name consists of each indexed field name concatenated with the key value for that field. For example, the sort order, or if it’s a text
index, the string _text
, or if it’s a 2dsphere
index, the string _2dsphere
, etc.
But you can assign your own name when creating indexes in MongoDB.
The Default Name
Suppose we create an index like this:
db.employees.createIndex(
{
name: 1,
salary: -1 }
)
We didn’t specify a name, so MongoDB will automatically generate one, based on the index specification.
We use getIndexes()
to verify this:
db.employees.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "name" : 1, "salary" : -1 }, "name" : "name_1_salary_-1" } ]
There are two indexes here. The first one is the default _id
index that is automatically created with the collection. The second one is the one we just created.
In this case, MongoDB gave the index a name of name_1_salary_-1
.
If we ever need to drop this index, we would either need to remember its name or its definition. Given the default name is based on the definition, it could get a little unwieldy if you have a more complex definition.
In any case, we can drop the above index like this:
db.employees.dropIndex("name_1_salary_-1")
Specify a Name
If we wanted to create our own name for the index, we could have created it like this:
db.employees.createIndex(
{
name: 1,
salary: -1
},
{
name: "employee_salary"
}
)
All we did was add a name
field within an optional document for supplying options for the index.
If we now check the index, we can see that it has the name that we supplied:
db.employees.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "name" : 1, "salary" : -1 }, "name" : "employee_salary" } ]
Including the Name with Other Options
In the previous example, the name
field is the only option that we provided when creating the index. The name isn’t the only option you can specify for an index.
You can also provide other options, such as the default language, language override, etc. Any other options will need to be enclosed within the same document, separated by a comma.
Here’s an example of creating an index with two other options (but without specifying the name):
db.sitcoms.createIndex(
{
"original_name": "text",
"translations.sitcom_name": "text"
},
{
"default_language": "danish",
"language_override": "sprog"
}
)
This is a different index on a different collection to the previous one. But you can see that we’ve specified our own default_language
and a language_override
values. It’s also a compound index (it’s an index that includes multiple fields).
I didn’t specify a name, so we can see how long the name becomes with such an index:
db.employees.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "_fts" : "text", "_ftsx" : 1 }, "name" : "original_name_text_translations.sitcom_name_text", "default_language" : "danish", "language_override" : "sprog", "weights" : { "original_name" : 1, "translations.sitcom_name" : 1 }, "textIndexVersion" : 3 } ]
So in this case MongoDB gave the index a default name of original_name_text_translations.sitcom_name_text
, because that’s derived from our index definition.
We would need to use the following code to drop this index by name:
db.sitcoms.dropIndex("original_name_text_translations.sitcom_name_text")
To include the name in such an index, we can provide it in the same document that we used to specify the default language and language override.
So we could create the index like this:
db.sitcoms.createIndex(
{
"original_name": "text",
"translations.sitcom_name": "text"
},
{
"default_language": "danish",
"language_override": "sprog",
"name": "sitcoms_da"
}
)
In this case we used sitcoms_da
as the index name.
Now when we get a list of indexes, we can see our specified name:
db.sitcoms.getIndexes()
Result:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "_fts" : "text", "_ftsx" : 1 }, "name" : "sitcoms_da", "default_language" : "danish", "language_override" : "sprog", "weights" : { "original_name" : 1, "translations.sitcom_name" : 1 }, "textIndexVersion" : 3 } ]