mongodb-query-and-index-optimize

Diagnose MongoDB query plans and design index and aggregation strategies.

Updated Dec 20, 2023
One-click install
npx skills add https://github.com/Thiago-Cruz-eng/Hibrygame --skill mongodb-query-and-index-optimize-thiago-cruz-eng
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mongodb-query-and-index-optimize
Source: https://github.com/Thiago-Cruz-eng/Hibrygame/tree/main/.claude/skills/mongodb-query-and-index-optimize
Command: npx skills add https://github.com/Thiago-Cruz-eng/Hibrygame --skill mongodb-query-and-index-optimize-thiago-cruz-eng

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It solves slow MongoDB performance caused by missing or poorly designed indexes, inefficient query patterns, and aggregation pipelines that waste work or memory.

Core Features & Use Cases

  • Index strategy that matches query shapes: Applies compound index field ordering (ESR), prefix principle, and compound vs single-field choices to avoid COLLSCAN and in-memory sorts.
  • Specialized index guidance: Recommends the correct index types for real workloads, including partial, sparse, TTL, unique, text, geospatial, hashed, wildcard, multikey, clustered, and hidden indexes.
  • Explain- and profiler-driven optimization: Teaches how to interpret executionStats to confirm IXSCAN vs COLLSCAN, diagnose scan-to-return inefficiency, and validate improvements.
  • Aggregation pipeline optimization: Improves stage ordering ($match/$project early), prevents document explosion ($unwind on large arrays), optimizes $lookup and recursive traversal ($graphLookup), and controls memory/spill behavior.
  • Production rollout and safety: Helps plan index builds to minimize impact, and offers diagnostics like $indexStats, slow query log, $queryStats, plan cache, and MongoDB 8.0 query settings.

Quick Start

Use this skill to optimize a slow MongoDB query by identifying the COLLSCAN reason, selecting the most suitable index pattern, and validating the improvement using explain("executionStats").

Frequently Asked Questions about mongodb-query-and-index-optimize

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix a slow MongoDB query showing COLLSCAN in explain() output?

To fix a slow MongoDB query showing COLLSCAN, you need to design a compound index applying the ESR rule and prefix principle, replacing collection scans with IXSCAN. This ensures the index field ordering matches your query shapes and eliminates inefficient in-memory sorts.

How do I optimize a MongoDB aggregation pipeline to prevent memory spills?

Optimize a MongoDB aggregation pipeline by moving $match and $project stages early to reduce document flow, controlling $unwind on large arrays to prevent document explosion, and tuning $lookup and $graphLookup stages to manage memory spill behavior efficiently.

When should I use partial or sparse indexes in MongoDB?

Use partial or sparse indexes in MongoDB when your workload queries a subset of documents meeting specific criteria, reducing index size and maintenance overhead. Specialized index selection also includes TTL, geospatial, text, hashed, wildcard, and multikey indexes tailored to real workload needs.

What is the best way to diagnose missing or unused indexes in MongoDB?

The best way to diagnose missing or unused indexes in MongoDB is by analyzing executionStats from explain, reviewing $indexStats, and monitoring the profiler and slow query logs. This validates scan-to-return inefficiency and identifies indexes that do not serve current query patterns.

How do I safely build indexes in a production MongoDB database?

Safely build indexes in production MongoDB by planning the rollout to minimize performance impact, using hidden indexes to test query plans before exposure, and verifying improvements through executionStats, plan cache, and MongoDB 8.0 query settings.

Why does my MongoDB query still perform an in-memory SORT despite having an index?

A MongoDB query still performs an in-memory SORT when the compound index field ordering does not align with the query and sort criteria. Correcting the index design using the ESR rule and prefix principle ensures the index can return documents in the required sorted order.