Index Management
Create, analyze, and manage MongoDB indexes with Sutido.
Overview
Indexes are critical for MongoDB query performance. Sutido lets you view existing indexes, create new ones, and analyze index usage to optimize your database.
Viewing Indexes
Indexes appear in the tree explorer under each collection. You can also query indexes directly:
// List all indexes on a collection
db.users.getIndexes() Index Types
Single Field Index
The most basic index type. Create on fields you frequently query:
// Create ascending index
db.users.createIndex({ email: 1 })
// Create descending index
db.users.createIndex({ createdAt: -1 }) Compound Index
Index multiple fields together. Order matters for query optimization:
// Good for queries on status, then sorting by date
db.orders.createIndex({ status: 1, createdAt: -1 }) Rule of thumb: Put equality conditions first, then sort fields, then range conditions.
Unique Index
Enforce uniqueness on a field:
db.users.createIndex({ email: 1 }, { unique: true }) Text Index
Enable full-text search:
db.articles.createIndex({ title: "text", content: "text" }) TTL Index
Automatically delete documents after a time period:
// Delete documents 30 days after createdAt
db.sessions.createIndex(
{ createdAt: 1 },
{ expireAfterSeconds: 2592000 }
) Geospatial Index
For location-based queries:
db.places.createIndex({ location: "2dsphere" }) Analyzing Index Usage
Index Statistics
// Get usage statistics for all indexes
db.users.aggregate([{ $indexStats: {} }]) Explain Query Plan
See which index a query uses:
db.users.find({ email: "test@example.com" }).explain("executionStats") Key Explain Fields
| Field | What to Look For |
|---|---|
winningPlan.stage | IXSCAN (good) vs COLLSCAN (bad) |
totalDocsExamined | Should be close to nReturned |
executionTimeMillis | Total query time |
indexName | Which index was used |
Creating Indexes
Basic Syntax
db.collection.createIndex(
{ field: 1 }, // Key pattern
{ option: value } // Options
) Common Options
| Option | Description |
|---|---|
unique | Enforce unique values |
sparse | Only index documents with the field |
background | Build in background (deprecated in 4.2+) |
name | Custom index name |
partialFilterExpression | Index only matching documents |
Partial Index Example
// Only index active users
db.users.createIndex(
{ email: 1 },
{ partialFilterExpression: { status: "active" } }
) Dropping Indexes
// Drop by name
db.users.dropIndex("email_1")
// Drop by key pattern
db.users.dropIndex({ email: 1 })
// Drop all indexes (except _id)
db.users.dropIndexes() Warning: Dropping indexes affects query performance. In production, test the impact first.
Index Best Practices
Index Selectively
Don't create indexes on every field. Focus on:
- Fields used in query filters
- Fields used for sorting
- Fields with high selectivity (many unique values)
Consider Write Performance
Every index slows down writes. Balance read performance gains against write overhead.
Monitor Index Size
// Check index sizes
db.users.stats().indexSizes Remove Unused Indexes
Use $indexStats to find indexes that aren't being used and consider removing them.
Covered Queries
A query is "covered" when all requested fields are in the index. These are very fast:
// Index on { email: 1, name: 1 }
// This query is covered - no document fetch needed
db.users.find(
{ email: "test@example.com" },
{ name: 1, _id: 0 }
) Next Steps
Customize Sutido in Settings & Config.