mongodb-query-optimizer

Analyze MongoDB explain plans and recommend compound indexes to reduce latency.

4|2|Updated May 18, 2022
One-click install
npx skills add https://github.com/pjmagee/starwars-data --skill mongodb-query-optimizer-pjmagee
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mongodb-query-optimizer
Source: https://github.com/pjmagee/starwars-data/tree/main/.agents/skills/mongodb-query-optimizer
Command: npx skills add https://github.com/pjmagee/starwars-data --skill mongodb-query-optimizer-pjmagee

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Helps developers and DBAs identify why MongoDB queries are slow and recommends targeted index and query-plan changes to reduce latency, scanned documents, and in-memory sorts.

Core Features & Use Cases

  • Explain plan analysis: Interpret explain() output to detect COLLSCAN, in-memory sorts, and high keys/documents examined ratios.
  • Index recommendations: Propose compound indexes following ESR/ERS ordering and advise when to remove or consolidate indexes based on Atlas Performance Advisor.
  • Cluster-level diagnosis: Use Atlas Performance Advisor and slowQueryLogs (when configured) to prioritize high-impact slow queries across a cluster.
  • Aggregation and update guidance: Provide pipeline optimizations and oplog-efficient update patterns when relevant.
  • Reference-driven: Loads core indexing principles and antipattern examples to ground recommendations in proven best practices.

Quick Start

Ask the skill to analyze the slow query db.orders.find({status: 'shipped', region: 'US'}).sort({date: -1}) and recommend the minimal index changes and reasoning.

Frequently Asked Questions about mongodb-query-optimizer

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

FAQPage Schema
How do I diagnose slow MongoDB queries using explain plan output?

Analyzing explain plans identifies MongoDB query bottlenecks like COLLSCAN or in-memory sorts by examining the keys and documents examined ratios, enabling targeted compound index recommendations to reduce latency.

What is the ESR rule for building compound indexes in MongoDB?

ESR ordering structures compound indexes by Equality, Sort, then Range fields. MongoDB query optimization applies this best practice to minimize scanned documents and prevent in-memory sorts.

How do I use Atlas Performance Advisor suggestedIndexes to improve MongoDB performance?

Atlas Performance Advisor provides slowQueryLogs and suggestedIndexes to prioritize high-impact slow queries across a cluster. The optimizer uses this data to advise when to create, consolidate, or remove indexes for better performance.

Can I optimize MongoDB aggregation pipelines to reduce resource consumption?

Yes, MongoDB query optimization provides pipeline guidance to reduce resource consumption. It analyzes aggregation stages and recommends oplog-efficient update patterns alongside index improvements when relevant.

What data do I need to provide to analyze MongoDB query performance?

You need to provide collection indexes, explain() output, or Atlas Performance Advisor logs. The optimizer uses this input to analyze slow queries and apply indexing best practices like ESR ordering.

Why does my MongoDB query keep doing a COLLSCAN instead of using my index?

A COLLSCAN occurs when MongoDB cannot use an existing index effectively, often due to mismatched compound index field ordering. Analyzing the explain plan and applying ESR-based index recommendations resolves this.