When you create an index, you can give the index a custom name. Giving your index a name helps distinguish different indexes on your collection. For example, you can more easily identify the indexes used by a query in the query plan's explain results if your indexes have distinct names.
To specify the index name, include the name option when you create the index:
db.<collection>.createIndex( { <field>: <value> }, { name: "<indexName>" } )
About this Task
Before you specify an index name, consider the following:
Index names must be unique. Creating an index with the name of an existing index returns an error.
You can't rename an existing index. Instead, you must drop and recreate the index with a new name.
Default Index Names
If you don't specify a name during index creation, the system generates the name by concatenating each index key field and value with underscores. For example:
Index | Default Name |
|---|---|
|
|
|
|
|
|
|
|
Procedure
The examples on this page use data from the sample_mflix sample dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the sample dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.
Create a text index on the name and text fields. Set the index name to CommentsTextIndex:
db.comments.createIndex( { name: "text", text: "text" }, { name: "CommentsTextIndex" } )
CommentsTextIndex
Results
After you create the index, you can use the db.collection.getIndexes() method to get the index name:
db.comments.getIndexes()
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { _fts: 'text', _ftsx: 1 }, name: 'CommentsTextIndex', weights: { name: 1, text: 1 }, default_language: 'english', language_override: 'language', textIndexVersion: 3 } ]
Learn More
To learn how to create an index, see Create an Index.
For more information about index properties, see Index Properties.