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.
db.users.createIndex({ email: 1 })A single-field index is often the first optimization for lookup-heavy fields.
db.orders.createIndex({ status: 1, created_at: -1 })Compound indexes are useful when queries filter and sort on a predictable pattern.
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.
db.orders.find({ status: "paid" }).sort({ created_at: -1 }).explain("executionStats")`executionStats` gives practical detail such as scanned docs, scanned keys, and returned docs.
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.
db.orders.hideIndex({ status: 1, created_at: -1 })Hidden indexes can help you test whether an index is still needed before dropping it.
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.