getIndexes() (List Indexes)
Last updated: 2026-10-09
getIndexes() (List Indexes)
getIndexes() returns all indexes of a collection, e.g. `db.users.getIndexes()`. Each entry shows the index name, keys, and uniqueness flags, with the _id index always first. It is the first tool to reach for when diagnosing query performance or confirming an index exists.
| Category | Index & Admin |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.getIndexes()
Returns:Array of index info documents with name, key, unique, etc.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// getIndexes: list all indexes on users
const indexes = db.users.getIndexes()
indexes
Output:
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{ v: 2, key: { age: 1 }, name: 'age_1' }
]
📌 Notes
- The _id index is always first
- Use it to confirm a createIndex actually took effect
- Missing indexes cause collection scans — inspect to decide what to add
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QHow do I confirm an index was created?
ACall db.users.getIndexes() and look for the index name in the list.
QWhy is _id always in the getIndexes result?
AThe _id index is system-created to enforce _id uniqueness and lookup performance; it is always present.
QHow do I check whether a query uses an index?
ARun .explain('executionStats') on the query and inspect fields like totalDocsExamined.