Master MongoDB index creation and types. Learn single-field, compound, unique, text, geospatial, and TTL indexes. Optimize query performance dramatically with proper indexing.
Creates strategic MongoDB indexes to optimize query performance. Use when you need to speed up slow queries, enforce uniqueness, or support full-text/geospatial searches.
/plugin marketplace add pluginagentmarketplace/custom-plugin-mongodb/plugin install mongodb-developer-plugin@pluginagentmarketplace-mongodbThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlreferences/GUIDE.mdscripts/helper.pyDramatically improve query performance with strategic indexing.
// Single field index
await collection.createIndex({ email: 1 })
// Compound index (order matters!)
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 })
// TTL index (auto-delete after 24 hours)
await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: 86400 })
// Text index (full-text search)
await collection.createIndex({ title: 'text', content: 'text' })
// Geospatial index
await collection.createIndex({ location: '2dsphere' })
// Simple index for one field
db.users.createIndex({ email: 1 })
// Benefits:
// - Speeds up queries on email field
// - Speeds up sorts on email
// - Speeds up range queries: { $gt, $lt }
// When to use:
// - Frequently filtered/sorted field
// - High cardinality (many unique values)
// Multiple fields - ORDER MATTERS!
db.orders.createIndex({ status: 1, createdAt: -1 })
// Good for queries like:
// { status: 'completed', createdAt: {$gt: date} }
// { status: 'completed' } // Can use this index
// Bad for:
// { createdAt: {$gt: date} } // Won't use index well
// Ensure field uniqueness
db.users.createIndex({ email: 1 }, { unique: true })
// Prevents duplicates:
// - insertOne with duplicate email → ERROR
// - Can't insert if email already exists
// Sparse unique (allow multiple nulls)
db.users.createIndex({ phone: 1 }, { unique: true, sparse: true })
// Skip documents where field is null/missing
db.users.createIndex({ phone: 1 }, { sparse: true })
// Benefits:
// - Smaller index (excludes nulls)
// - Matches queries with { $exists: true }
// When to use:
// - Optional fields (not all documents have it)
// - Reduce index size
// Full-text search
db.articles.createIndex({
title: 'text',
content: 'text',
tags: 'text'
})
// Query:
db.articles.find({ $text: { $search: 'mongodb' } })
// Weights (title more important than content)
db.articles.createIndex({
title: 'text',
content: 'text'
}, { weights: { title: 10, content: 5 } })
// 2D spherical (lat/long)
db.venues.createIndex({ location: '2dsphere' })
// Query nearby
db.venues.find({
location: {
$near: {
type: 'Point',
coordinates: [-73.97, 40.77]
},
$maxDistance: 5000 // 5km
}
})
// Auto-delete documents after time period
db.sessions.createIndex({ createdAt: 1 }, {
expireAfterSeconds: 3600 // 1 hour
})
// Use cases:
// - Sessions that expire
// - Temporary logs
// - Cache-like collections
// MongoDB checks once per minute
// Deletion might lag up to 1 minute
// Show all indexes
const indexes = await collection.indexes()
console.log(indexes)
// Shows: name, key, size, doc count
// Drop specific index
await collection.dropIndex('email_1')
// Drop all non-_id indexes
await collection.dropIndexes()
await collection.createIndex({ email: 1 }, {
unique: true, // Enforce uniqueness
sparse: true, // Skip nulls
background: true, // Don't block writes
expireAfterSeconds: 86400, // TTL
collation: { locale: 'en' }, // Language-specific
name: 'custom_name' // Custom index name
})
Equality, Sort, Range - optimal compound index order
// Query: Find active users sorted by created date, age 18-65
db.users.find({
status: 'active', // Equality
age: { $gte: 18, $lte: 65 } // Range
}).sort({ createdAt: -1 }) // Sort
// Optimal index:
db.users.createIndex({
status: 1, // Equality first
createdAt: -1, // Sort second
age: 1 // Range last
})
// Query returns entirely from index, no documents fetched!
// Create index with all needed fields
db.users.createIndex({ email: 1, name: 1, age: 1 })
// Query (covered - no docs fetched)
db.users.find(
{ email: 'user@example.com' },
{ projection: { email: 1, name: 1, age: 1, _id: 0 } }
)
// Much faster than fetching documents!
// MongoDB 4.4+
db.collection.aggregate([
{ $indexStats: {} }
])
// Shows:
// - accesses.ops: Number of operations using index
// - accesses.since: When index was created
// Identify unused:
// - Low accesses.ops value
// - Recent accesses.since date
// Drop unused indexes:
await collection.dropIndex('unused_index_name')
✅ Index Design:
✅ Performance:
❌ Avoid:
Ready to speed up your MongoDB! ⚡
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.