ESR Rule

noun

say it: “EE-ES-AR ROOL” /ˌiˌɛsˈɑr ˈrul/

A guideline for ordering the fields of a MongoDB index on several fields: exact matches first, then the sort field, then ranges.

What is the ESR rule?

The ESR rule is the order for a compound index's fields: Equality fields (matched exactly) first, then the Sort field, then Range fields (such as $gte). In that order, the index finds the matches already sorted.

// for: find({ status: "shipped", total: { $gte: 100 } }).sort({ placedAt: -1 })
db.orders.createIndex({ status: 1, placedAt: -1, total: 1 });
//                      equality   sort           range

In a sentence

  • “The ESR rule meant the sorted results came straight from the index, with no sort in memory.”
  • “Following the ESR rule, status came first in the index, placedAt second and total last.”

Level: Freelancer · More MongoDB terms at this level

Updated