ESR: equality, sort, range
coreintermediateESR is a naming convention for the ordering rule section 10 introduced: put Equality fields first, Sort fields second, Range fields last, when designing a compound index for a specific query. It is a repeatable procedure, not a one-off insight.
Think of it as
ESR turns "design a good compound index" from a judgment call into a checklist: read the query, label every field E (exact match), S (used in .sort()), or R (range/$in/$gt/etc.), then write the index in that label order. Two engineers applying ESR to the same query independently should converge on the same index.
What we're doing: Apply ESR to a query where the fields appear in a different order than the index should use.
- 1
- The query happens to write total (a range field) before status (an equality field) — the order fields appear in the query has no bearing on index order.
- 5
- ESR classification, not field-appearance order, decides the index: status (E) first, createdAt (S) second, total (R) last.
Why this works: ESR deliberately ignores the order a query happens to list its conditions in — the classification (what role each field plays) is what determines a good index, which is precisely why it produces the same answer regardless of how the query was typed.
Building the compound index in the same field order the query happens to be written in
Wrong
Better
What you see: A compound index exists that "has all the right fields," but explain() still shows a SORT stage or a larger-than-expected totalDocsExamined.
Why: A query's field-writing order is an accident of how someone typed the filter — the field's actual role (equality, sort, or range) is what the index needs to reflect, and those two orderings frequently disagree.
- read the query — find + sort
- label each field — E, S, or R
- index in E-S-R order — regardless of query field order
Classifying a query's fields for ESR
Together
Remember: ESR: classify each field as Equality, Sort, or Range for the target query, then build the compound index in that order — regardless of how the query happens to be written.
See also: key ordering and query shapes · designing from real query patterns

