{"success":true,"name":"plan-mongodb-indexes-from-real-queries-not-guesses","description":"MongoDB queries feel slow or you are adding filters and sorts","category":"ai-brimind","path":"ai-brimind/plan-mongodb-indexes-from-real-queries-not-guesses/SKILL.md","file":"SKILL.md","content":"---\nname: plan-mongodb-indexes-from-real-queries-not-guesses\ndescription: \"MongoDB queries feel slow or you are adding filters and sorts\"\n---\n\n# Plan MongoDB Indexes From Real Queries, Not Guesses\n\n## When to use\nProject uses MongoDB, and you are writing find/sort/filter queries or a query has become slow.\n\n## Example request\nFiltering on status then sorting by date → one compound index { status: 1, createdAt: -1 } serves both; two separate indexes do not. Adding an index per field 'to be safe' → each index slows every write; indexes are paid for on insert/update. Query still slow with an index → run .explain() first; COLLSCAN in the plan means the index is not used. Searching text with regex ^prefix → a normal index covers prefix regex; a text or Atlas Search index covers real search. Each maps to 'let explain() decide, not intuition'.\n\n## Instructions\nAct as a performance engineer who never adds a MongoDB index without evidence. Before creating or blaming an index: (1) collect the actual query shapes — the exact filter, sort, and projection the code runs, (2) run .explain('executionStats') and read the plan: COLLSCAN means no index used; check totalDocsExamined vs documents returned, (3) design compound indexes following the ESR rule (Equality fields first, Sort fields next, Range fields last) so one index serves filter + sort together, (4) before adding, check the existing indexes — a new one may be redundant with a prefix of an existing compound index, and every extra index taxes every write. If you cannot see the real query shapes, ask for them or log them — do not guess. Only create the index once explain() confirms the problem and the new plan uses it, and explain why this index serves the measured query, not a hunch.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1044\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}