{"success":true,"name":"mongodb-compound-index-order-decides-everything-equality-sor","description":"Adding a MongoDB index for a query that filters and sorts on several fields","category":"ai-brimind","path":"ai-brimind/mongodb-compound-index-order-decides-everything-equality-sor/SKILL.md","file":"SKILL.md","content":"---\nname: mongodb-compound-index-order-decides-everything-equality-sor\ndescription: \"Adding a MongoDB index for a query that filters and sorts on several fields\"\n---\n\n# MongoDB Compound Index Order Decides Everything: Equality, Sort, Then Range\n\n## When to use\nProject uses MongoDB/Mongoose, and you're creating a compound index or a filtered-and-sorted query is slow despite 'having an index'.\n\n## Example request\nIndex on {date, clubId} for 'this club's recent bookings' → the equality field trails the range field and the index barely helps; equality fields must come first. — Query sorts by createdAt but the index puts it last after a range field → Mongo sorts in memory and slows or errors on big results; sort fields belong between equality and range (the ESR rule). — Index on {a, b} expected to serve a query on {b} alone → it can't; a compound index only serves queries using its prefix fields, left to right. — Ten single-field indexes 'to be safe' → writes slow and none matches the real query; one well-ordered compound index built from the actual query beats a pile of guesses.\n\n## Instructions\nAct as a librarian designing the shelf order: books sorted by author-then-year answer 'all books by X, newest first' instantly, but sorted year-then-author they answer nothing fast. When indexing a MongoDB query: 1) Write down the actual query first — its equality filters (field: value), its sort, and its range conditions ($gt, $lt, $in ranges, dates) — the index is designed FROM this, never guessed. 2) Order the compound index by the ESR rule: Equality fields first, Sort fields second, Range fields last — this single ordering rule explains most 'I have an index but it's slow' mysteries. 3) Remember prefixes: a compound index serves only queries that use its leading fields; check whether an existing index already covers your query as a prefix before adding a new one — fewer, better-ordered indexes beat many overlapping ones, because every index taxes every write. 4) Verify with explain(): confirm the winning plan uses the index (IXSCAN), examines a document count close to what it returns, and shows no in-memory SORT stage — explain is the proof, guessing is not. 5) In multi-tenant collections, the tenant field is an equality filter on every query and almost always belongs first. If the slow query's real shape is unclear, capture it from logs before designing anything. Only keep the index once explain() confirms it serves the real query, and explain why the field order matches equality-sort-range rather than just listing the fields the query mentions.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #969\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}