Create a single-field index
Index one frequently filtered field.
db.users.createIndex({ email: 1 })A single-field index is often the first optimization for lookup-heavy fields.
Create indexes, inspect query plans, and troubleshoot common MongoDB query performance issues.
The most common index patterns for apps and APIs.
Index one frequently filtered field.
db.users.createIndex({ email: 1 })A single-field index is often the first optimization for lookup-heavy fields.
Index fields used together in filters or sorts.
db.orders.createIndex({ status: 1, created_at: -1 })Compound indexes are useful when queries filter and sort on a predictable pattern.
Prevent duplicate values at write time.
db.users.createIndex({ email: 1 }, { unique: true })Great for usernames, emails, external ids, and other unique keys.
db.articles.createIndex({ title: "text", body: "text" })Text indexes support MongoDB text search syntax over indexed fields.
db.users.getIndexes()Useful before adding more indexes or debugging an execution plan.
See what the query planner is doing and validate index usage.
Show the execution plan for a `find()` query.
db.orders.find({ status: "paid" }).sort({ created_at: -1 }).explain("executionStats")`executionStats` gives practical detail such as scanned docs, scanned keys, and returned docs.
Test how a query behaves with a chosen index.
db.orders.find({ status: "paid" }).hint({ status: 1, created_at: -1 })Helpful for diagnostics, but avoid overusing hints in app code unless there is a strong reason.
Temporarily hide an index from the planner.
db.orders.hideIndex({ status: 1, created_at: -1 })Hidden indexes can help you test whether an index is still needed before dropping it.
View storage and index metrics for a collection.
db.runCommand({ collStats: "orders" })Useful for checking total size, avg document size, and index footprint.
db.orders.dropIndex({ status: 1, created_at: -1 })Every index speeds some reads but adds write and storage cost, so removing unused ones can help.