home / skills / pluginagentmarketplace / custom-plugin-mongodb / mongodb-indexing

mongodb-indexing skill

/skills/mongodb-indexing

This skill helps you optimize MongoDB performance by mastering indexes, explain plans, and ESR-compliant designs to speed queries.

npx playbooks add skill pluginagentmarketplace/custom-plugin-mongodb --skill mongodb-indexing

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
5.7 KB
---
name: mongodb-indexing-optimization
version: "2.1.0"
description: Master MongoDB indexing and query optimization. Learn index types, explain plans, performance tuning, and query analysis. Use when optimizing slow queries, analyzing performance, or designing indexes.
sasmp_version: "1.3.0"
bonded_agent: 04-mongodb-performance-indexing
bond_type: PRIMARY_BOND

# Production-Grade Skill Configuration
capabilities:
  - index-design
  - explain-analysis
  - query-optimization
  - performance-profiling
  - esr-rule-application

input_validation:
  required_context:
    - query_pattern
    - collection_info
  optional_context:
    - explain_output
    - current_indexes
    - collection_size

output_format:
  index_recommendation: object
  explain_interpretation: string
  performance_impact: string
  trade_offs: array

error_handling:
  common_errors:
    - code: IDX001
      condition: "COLLSCAN in explain output"
      recovery: "Create index on filter fields following ESR rule"
    - code: IDX002
      condition: "Index not being used"
      recovery: "Check query shape, hint usage, or index prefix"
    - code: IDX003
      condition: "Too many indexes"
      recovery: "Review index usage stats, remove unused indexes"

prerequisites:
  mongodb_version: "4.4+"
  required_knowledge:
    - basic-queries
    - explain-command
  performance_baseline:
    - "Know current query latencies before optimization"

testing:
  unit_test_template: |
    // Verify index is used
    const explain = await collection.find(query).explain('executionStats')
    expect(explain.executionStats.executionStages.stage).toBe('IXSCAN')
    expect(explain.executionStats.totalDocsExamined).toBeLessThanOrEqual(limit)
---

# MongoDB Indexing & Optimization

Master performance optimization through proper indexing.

## Quick Start

### Create Indexes
```javascript
// Single field index
await collection.createIndex({ email: 1 });

// Compound index
await collection.createIndex({ status: 1, createdAt: -1 });

// Unique index
await collection.createIndex({ email: 1 }, { unique: true });

// Sparse index (skip null values)
await collection.createIndex({ phone: 1 }, { sparse: true });

// Text index (full-text search)
await collection.createIndex({ title: 'text', description: 'text' });

// TTL index (auto-delete documents)
await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 });
```

### List and Analyze Indexes
```javascript
// List all indexes
const indexes = await collection.indexes();
console.log(indexes);

// Drop an index
await collection.dropIndex('email_1');

// Drop all non-_id indexes
await collection.dropIndexes();
```

### Explain Query Plan
```javascript
// Analyze query execution
const explain = await collection.find({ email: '[email protected]' }).explain('executionStats');

console.log(explain.executionStats);
// Shows: executionStages, nReturned, totalDocsExamined, executionTimeMillis
```

## Index Types

### Single Field Index
```javascript
// Index on one field
db.collection.createIndex({ age: 1 })

// Query uses index if searching on age
db.collection.find({ age: { $gte: 18 } })
```

### Compound Index
```javascript
// Index on multiple fields - order matters!
db.collection.createIndex({ status: 1, createdAt: -1 })

// Queries that benefit:
// 1. { status: 'active', createdAt: { $gt: date } }
// 2. { status: 'active' }
// But NOT: { createdAt: { $gt: date } } alone
```

### Array Index (Multikey)
```javascript
// Automatically created for arrays
db.collection.createIndex({ tags: 1 })

// Matches documents where tags contains value
db.collection.find({ tags: 'mongodb' })
```

### Text Index
```javascript
// Full-text search
db.collection.createIndex({ title: 'text', body: 'text' })

// Query
db.collection.find({ $text: { $search: 'mongodb database' } })
```

### Geospatial Index
```javascript
// 2D spherical for lat/long
db.collection.createIndex({ location: '2dsphere' })

// Find nearby
db.collection.find({
  location: {
    $near: { type: 'Point', coordinates: [-73.97, 40.77] },
    $maxDistance: 5000
  }
})
```

## Index Design: ESR Rule

**E**quality, **S**ort, **R**ange

```javascript
// Query: find active users, sort by created date, limit age
db.users.find({
  status: 'active',
  age: { $gte: 18 }
}).sort({ createdAt: -1 })

// Optimal index:
db.users.createIndex({
  status: 1,      // Equality
  createdAt: -1,  // Sort
  age: 1          // Range
})
```

## Performance Analysis

### Check if Query Uses Index
```javascript
const explain = await collection.find({ email: '[email protected]' }).explain('executionStats');

// IXSCAN = Good (index scan)
// COLLSCAN = Bad (collection scan)
console.log(explain.executionStats.executionStages.stage);
```

### Covering Query
```javascript
// Query results entirely from index
db.users.createIndex({ email: 1, name: 1, _id: 1 })

// This query is "covered" - no need to fetch documents
db.users.find({ email: '[email protected]' }, { email: 1, name: 1, _id: 0 })
```

## Python Examples

```python
from pymongo import ASCENDING, DESCENDING

# Create index
collection.create_index([('email', ASCENDING)], unique=True)

# Compound index
collection.create_index([('status', ASCENDING), ('createdAt', DESCENDING)])

# Explain plan
explain = collection.find({'email': '[email protected]'}).explain()
print(explain['executionStats'])

# Drop index
collection.drop_index('email_1')
```

## Best Practices

✅ Index fields used in $match (early in pipeline)
✅ Use ESR rule for compound indexes
✅ Monitor index size and memory
✅ Remove unused indexes
✅ Use explain() to verify index usage
✅ Index strings with high cardinality
✅ Avoid indexing fields with many nulls
✅ Consider index intersection
✅ Regular index maintenance
✅ Monitor query performance

Overview

This skill teaches MongoDB indexing and query optimization to improve read and write performance. It covers index types, ESR-based compound index design, explain plans, and practical tuning steps. Use it to diagnose slow queries, design efficient indexes, and reduce collection scans.

How this skill works

The skill inspects queries and collection schemas, recommends index types (single, compound, multikey, text, geospatial, TTL) and shows how to create or drop indexes using drivers like PyMongo. It explains explain() output and identifies IXSCAN vs COLLSCAN, covering queries, index intersection opportunities, and index size considerations. You get concrete examples and Python snippets to apply changes and verify improvements with executionStats.

When to use it

  • When queries are slow or reports show high executionTimeMillis
  • When designing new collections to support common query patterns
  • Before deploying large-scale changes to read-heavy workloads
  • When indexes consume too much memory or disks and need pruning
  • When creating search, geospatial, or TTL-based lifecycle policies

Best practices

  • Apply the ESR rule: Equality fields first, then Sort fields, then Range fields
  • Use explain('executionStats') to confirm IXSCAN and check totalDocsExamined
  • Prefer covering indexes for frequent read projections to avoid fetches
  • Monitor index size and remove unused indexes to save RAM
  • Avoid indexing low-cardinality or highly null fields; consider sparse or partial indexes
  • Build compound indexes in the correct field order; consider index intersection only when necessary

Example use cases

  • Optimize a login or lookup query by adding a single-field unique index on email
  • Speed up paginated feeds by creating a compound index on status and createdAt
  • Implement full-text search with text indexes for title and description fields
  • Support geolocation features using 2dsphere indexes and $near queries
  • Automatically expire session data using TTL indexes on createdAt

FAQ

How do I know if a query is using an index?

Run explain('executionStats') and check the executionStages.stage; IXSCAN indicates the query used an index, COLLSCAN indicates a full collection scan.

When should I create a compound index versus multiple single-field indexes?

Create a compound index when queries filter and sort on multiple fields together (use ESR ordering). Rely on index intersection only if creating the compound index is not feasible.