Get the FREE Ultimate OpenClaw Setup Guide →

mongodb-query-and-index-optimize

npx machina-cli add skill romiluz13/mongodb-agent-skills/mongodb-query-and-index-optimize --openclaw
Files (1)
SKILL.md
9.4 KB

MongoDB Query and Index Optimization

Query patterns and indexing strategies for MongoDB, maintained by MongoDB. Contains 46 rules across 5 categories, prioritized by impact. Includes MongoDB 8.0 features: bulkWrite command, $queryStats (introduced in MongoDB 6.0.7, with 8.1/8.2 enhancements), Query Settings, and updateOne sort option. Indexes are the primary tool for query performance—most slow queries are missing an appropriate index.

When to Apply

Reference these guidelines when:

  • Writing new MongoDB queries or aggregations
  • Creating or reviewing indexes for collections
  • Debugging slow queries (COLLSCAN, high execution time)
  • Reviewing explain() output
  • Seeing Performance Advisor suggestions
  • Optimizing aggregation pipelines
  • Implementing full-text search
  • Adding geospatial queries
  • Setting up TTL (time-to-live) for data expiration
  • Analyzing index usage with $indexStats
  • Profiling slow operations

Rule Categories by Priority

PriorityCategoryImpactPrefixRules
1Index EssentialsCRITICALindex-9
2Specialized IndexesHIGHindex-11
3Query PatternsHIGHquery-10
4Aggregation OptimizationHIGHagg-8
5Performance DiagnosticsMEDIUMperf-8

Quick Reference

1. Index Essentials (CRITICAL) - 9 rules

  • index-compound-field-order - Equality first, sort second, range last (ESR rule)
  • index-compound-multi-field - Use compound indexes for multi-field queries
  • index-ensure-usage - Avoid COLLSCAN, verify with explain()
  • index-remove-unused - Audit indexes with $indexStats
  • index-high-cardinality-first - Put selective fields at index start
  • index-covered-queries - Include projected fields to avoid document fetch
  • index-prefix-principle - Compound indexes serve prefix queries
  • index-creation-background - Build indexes without blocking operations
  • index-size-considerations - Keep indexes in RAM for optimal performance

2. Specialized Indexes (HIGH) - 11 rules

  • index-unique - Enforce uniqueness for identifiers and constraints
  • index-partial - Index subset of documents to reduce size
  • index-sparse - Skip documents missing the indexed field
  • index-ttl - Automatic document expiration for sessions/logs
  • index-text-search - Full-text search with stemming and relevance
  • index-wildcard - Dynamic field indexing for polymorphic schemas
  • index-multikey - Array field indexing (one entry per element)
  • index-geospatial - 2dsphere indexes for location queries
  • index-hashed - Uniform distribution for equality lookups or shard keys
  • index-clustered - Ordered storage with clustered collections
  • index-hidden - Safely test index removals in production

3. Query Patterns (HIGH) - 10 rules

  • query-use-projection - Fetch only needed fields
  • query-avoid-ne-nin - Use $in instead of negation operators
  • query-or-index - All $or clauses must have indexes for index usage
  • query-anchored-regex - Start regex with ^ for index usage
  • query-batch-operations - Avoid N+1 patterns, use $in or $lookup
  • query-pagination - Use range-based pagination, not skip
  • query-exists-with-sparse - Understand $exists behavior with sparse indexes
  • query-sort-collation - Match sort order and collation to indexes
  • query-bulkwrite-command - MongoDB 8.0 cross-collection atomic batch operations
  • query-updateone-sort - MongoDB 8.0 deterministic updates with sort option

4. Aggregation Optimization (HIGH) - 8 rules

  • agg-match-early - Filter with $match at pipeline start
  • agg-project-early - Reduce document size with $project
  • agg-sort-limit - Combine $sort with $limit for top-N
  • agg-lookup-index - Ensure $lookup foreign field is indexed
  • agg-graphlookup - Use $graphLookup for recursive graph traversal
  • agg-avoid-large-unwind - Don't $unwind massive arrays
  • agg-allowdiskuse - Handle large aggregations exceeding 100MB
  • agg-group-memory-limit - Control $group memory and spills

5. Performance Diagnostics (MEDIUM) - 8 rules

  • perf-explain-interpretation - Read explain() output like a pro
  • perf-slow-query-log - Use profiler to find slow operations
  • perf-index-stats - Find unused indexes with $indexStats
  • perf-query-plan-cache - Understand and manage query plan cache
  • perf-use-hint - Force a known-good index when the optimizer errs
  • perf-atlas-performance-advisor - Use Atlas suggestions for missing indexes
  • perf-query-stats - Workload-based query analysis with $queryStats (introduced in MongoDB 6.0.7, with 8.1/8.2 enhancements)
  • perf-query-settings - MongoDB 8.0 persistent index hints with setQuerySettings

Key Principle

"If there's no index, it's a collection scan."

Every query without a supporting index scans the entire collection. A 10ms query on 10,000 documents becomes a 10-second query on 10 million documents.

ESR Rule (Equality-Sort-Range)

The most important rule for compound index field order:

// Query: status = "active" AND createdAt > lastWeek ORDER BY priority
// ESR: Equality (status) → Sort (priority) → Range (createdAt)
db.tasks.createIndex({ status: 1, priority: 1, createdAt: 1 })
PositionTypeExampleWhy
FirstEqualitystatus: "active"Narrows to exact matches
SecondSortORDER BY priorityAvoids in-memory sort
ThirdRangecreatedAt > dateScans within sorted data

ERS Exception: When range predicate is highly selective, placing Range before Sort reduces sort input. Verify with explain().

How to Use

Read individual rule files for detailed explanations and code examples:

rules/index-compound-field-order.md
rules/perf-explain-interpretation.md
rules/_sections.md

Each rule file contains:

  • Brief explanation of why it matters
  • Incorrect code example with explanation
  • Correct code example with explanation
  • "When NOT to use" exceptions
  • How to verify with explain()
  • Performance impact and metrics

How These Rules Work

Recommendations with Verification

Every rule in this skill provides:

  1. A recommendation based on best practices
  2. A verification checklist of things that should be confirmed
  3. Commands to verify so you can check before implementing
  4. MCP integration for automatic verification when connected

Why Verification Matters

I analyze code patterns, but I can't see your actual database without a connection. This means I might suggest:

  • Creating an index that already exists
  • Optimizing a query that's already using an efficient index
  • Adding a compound index when a prefix already covers the query

Always verify before implementing. Each rule includes verification commands.

MongoDB MCP Integration

For automatic verification, connect the MongoDB MCP Server:

Option 1: Connection String

{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": ["-y", "mongodb-mcp-server", "--readOnly"],
      "env": {
        "MDB_MCP_CONNECTION_STRING": "mongodb+srv://user:pass@cluster.mongodb.net/mydb"
      }
    }
  }
}

Option 2: Local MongoDB

{
  "mcpServers": {
    "mongodb": {
      "command": "npx",
      "args": ["-y", "mongodb-mcp-server", "--readOnly"],
      "env": {
        "MDB_MCP_CONNECTION_STRING": "mongodb://localhost:27017/mydb"
      }
    }
  }
}

⚠️ Security: Use --readOnly for safety. Remove only if you need write operations.

When connected, I can automatically:

  • Check existing indexes via mcp__mongodb__collection-indexes
  • Analyze query performance via mcp__mongodb__explain
  • Verify data patterns via mcp__mongodb__aggregate

⚠️ Action Policy

I will NEVER execute write operations without your explicit approval.

Operation TypeMCP ToolsAction
Read (Safe)find, aggregate, explain, collection-indexes, $indexStatsI may run automatically to verify
Write (Requires Approval)create-index, drop-index, update-many, delete-manyI will show the command and wait for your "yes"
Destructive (Requires Approval)drop-collection, drop-databaseI will warn you and require explicit confirmation

When I recommend creating an index or making changes:

  1. I'll explain what I want to do and why
  2. I'll show you the exact command
  3. I'll wait for your approval before executing
  4. If you say "go ahead" or "yes", only then will I run it

Your database, your decision. I'm here to advise, not to act unilaterally.

Working Together

If you're not sure about a recommendation:

  1. Run the verification commands I provide
  2. Share the output with me
  3. I'll adjust my recommendation based on your actual data

We're a team—let's get this right together.


Full Compiled Document

For the complete guide with all rules expanded: AGENTS.md

Source

git clone https://github.com/romiluz13/mongodb-agent-skills/blob/main/plugins/mongodb-agent-skills/skills/mongodb-query-and-index-optimize/SKILL.mdView on GitHub

Overview

Practical techniques to speed up MongoDB queries through smart indexing, explain analysis, and rule-based optimization. It covers compound, partial, text, and geospatial indexes, plus tools like $indexStats and profiler to diagnose slow operations.

How This Skill Works

The skill blends query-pattern analysis with index design principles, guiding the builder to create effective indexes (including ESR-compliant compound indexes, partial indexes, and text/geospatial indexes) and to verify changes using explain(), $indexStats, and the profiler.

When to Use It

  • When writing new MongoDB queries or aggregations that could benefit from indexing
  • When creating or reviewing indexes for a collection to improve read performance
  • When debugging slow queries or COLLSCAN scenarios and performance issues
  • When reviewing explain() output or Performance Advisor suggestions to optimize access patterns
  • When optimizing aggregation pipelines, full-text search, geospatial queries, or TTL index configurations

Quick Start

  1. Step 1: Run explain() on slow queries to identify whether COLLSCAN is occurring
  2. Step 2: Create or adjust indexes (e.g., compound, partial, text, geospatial) aligned with query patterns
  3. Step 3: Verify improvements with explain(), $indexStats, and profiler; test in staging before production

Best Practices

  • Follow ESR guidelines: equality first, sort second, range last in compound indexes
  • Design queries to be covered by indexes by including projected fields in the index
  • Place high-cardinality/selective fields at the beginning of compound indexes
  • Regularly audit indexes with $indexStats and remove unused or redundant indexes
  • Validate index changes with explain(), profiler, and cautious testing in staging before production

Example Use Cases

  • Use a compound index { email: 1, status: 1 } to efficiently support user lookup by email and status
  • Create a text index on { title: 'text', description: 'text' } for relevant full-text search results
  • Apply a 2dsphere index on { location: '2dsphere' } to accelerate near/within geospatial queries
  • Add a TTL index on { expiresAt: 1 } to automatically purge expired session/log documents
  • Use a sparse index on { lastLogin: 1 } to skip documents missing the field and reduce index size

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers