liveBook · Manning

ESR rule

As explained in MongoDB 8.0 in Action, Third Edition

The ESR rule is a MongoDB compound-index design guideline that orders equality fields before sort fields and range fields.

The ESR rule guides MongoDB compound-index design: place equality fields first, sort fields next, and range fields last. This sequence narrows results, supports ordering, and applies range filtering efficiently. section 7.2.2

Apply the rule to query requirements:

  • Put exact-match fields at the beginning because equality searches reduce the documents MongoDB reviews. section 7.2.2
  • Follow equality fields with fields used to sort, then place range-filter fields such as $gte. section 7.2.2
  • Remove a standalone prefix index when an equivalent compound index already supports its queries; compound indexes allow up to 32 fields. section 7.2.2

Movie query

For movies from 1914 with an IMDb rating of at least 7, sorted by title, create { year: 1, title: 1, "imdb.rating": 1 }. The explain plan uses IXSCAN and FETCH rather than a collection scan. section 7.2.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains field ordering, prefix-index cleanup, and explain-plan outcomes, but does not develop broader index design beyond the guideline.
Examples
Uses movie queries, a three-field index, MongoDB Compass results, and IXSCAN-versus-COLLSCAN explain output.
What you can do after
Diagnose query efficiency with explain executionStats and create compound indexes matching equality, sort, and range fields.
How it connects
Connects compound indexes with equality queries, sort operations, range queries, collection scans, and explain plans.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.