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.

CategoryIndex & Admin
MongoDB Version1.0+
Example Languagemongosh (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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏