Create and optimize MongoDB indexes for query performance
✓Works with OpenClaudeYou are a MongoDB performance engineer. The user wants to create and optimize MongoDB indexes to improve query performance.
What to check first
- Run
db.collection.getIndexes()to see existing indexes on your target collection - Check your query patterns with
db.collection.explain("executionStats")to identify slow queries - Review current database stats with
db.stats()anddb.collection.stats()to understand data volume
Steps
- Analyze your most-used queries using the MongoDB profiler: enable with
db.setProfilingLevel(1)and check slow queries insystem.profilecollection - Identify query predicates (find conditions, sort fields, projection fields) and determine index composition order: equality, sort, range
- Create a simple index on a single field with
db.collection.createIndex({ fieldName: 1 })where1is ascending and-1is descending - Create compound indexes for multi-field queries using
db.collection.createIndex({ field1: 1, field2: 1, field3: 1 })following the ESR rule (Equality, Sort, Range) - Use
db.collection.explain("executionStats").executionStats.executionStages.stageto verify index usage—look forCOLLSCAN(bad) vsIXSCAN(good) - Create partial indexes for filtered queries with
db.collection.createIndex({ status: 1 }, { partialFilterExpression: { active: true } })to reduce index size - Monitor index efficiency with
db.collection.aggregate([{ $indexStats: {} }]to identify unused or rarely-used indexes - Remove redundant indexes using
db.collection.dropIndex("indexName")and consolidate overlapping indexes into compound indexes
Code
const { MongoClient } = require("mongodb");
async function optimizeIndexes() {
const client = new MongoClient("mongodb://localhost:27017");
try {
await client.connect();
const db = client.db("myapp");
const collection = db.collection("users");
// 1. Check existing indexes
const existingIndexes = await collection.getIndexes();
console.log("Existing indexes:", existingIndexes);
// 2. Analyze query performance before indexing
const explainBefore = await collection.find({ email: "test@example.com" }).explain("executionStats");
console.log("Execution stage before:", explainBefore.executionStats.executionStages.stage);
// 3. Create single-field index
await collection.createIndex({ email: 1 });
console.log("Created index on email");
// 4. Create compound index following ESR rule
// Equality: status, Sort: createdAt, Range: age
await collection.createIndex(
{ status: 1, createdAt: -1, age: 1
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Database Skills
Other Claude Code skills in the same category — free to download.
Migration Generator
Generate database migration files
Query Optimizer
Analyze and optimize slow database queries
Schema Designer
Design database schema from requirements
Seed Data Generator
Generate database seed/sample data
Index Advisor
Suggest database indexes based on query patterns
ORM Model Generator
Generate ORM models from database schema
SQL to ORM
Convert raw SQL queries to ORM syntax
Database Backup Script
Create database backup and restore scripts
Want a Database skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.