# 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: **E**quality fields (matched exactly) first, then the **S**ort field, then **R**ange fields (such as `$gte`). In that order, the index finds the matches already sorted.

```javascript
// 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](https://www.ask-codey.com/glossary?category=mongodb&level=4)

Updated: November 19, 2026

Source: https://www.ask-codey.com/glossary/esr-rule
